Sign in to follow this  
Googol PL3X

luaL_openlibs(L) Linking problems

Recommended Posts

I've downloaded and installed lua version 5.1.4.18 from the lua.org site. I'm running Visual Studio C++ 2008 and have included the lib's and includes at tools->options->projects and solutions->VC++ Directories and added lua5.1.lib and lua51.lib for the projects linker input dependencies and I still get a bunch of unresolved external symbol errors on the following code:
#include <stdio.h>
#include <string.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

int main (void) {
  char buff[256];
  int error;
  lua_State *L = lua_open();   /* opens Lua */
  luaL_openlibs(L);

  while (fgets(buff, sizeof(buff), stdin) != NULL) {
    error = luaL_loadbuffer(L, buff, strlen(buff), "line") ||
            lua_pcall(L, 0, 0, 0);
    if (error) {
      fprintf(stderr, "%s", lua_tostring(L, -1));
      lua_pop(L, 1);
    }
  }

  lua_close(L);
  return 0;
}

Share this post


Link to post
Share on other sites
C++ and C, while very similar, are actually different languages. C++ uses a technique called name mangling to generate function names (hidden from the coder), while C dosn't do anything of the sort.

Since you were loading a C .lib file, with C code from a C++ project without using extern "C", the compiler assumed that your code was in C++, and mangled your names for you. This made it impossible to find the newly mangled names in the library file, resulting in unresolved externals.

Normally, you would solve this by putting extern "C" around the stuff that you knew was in C. However, since lua is by itself, a C library, they didn't do that. What they did do, was provide lua.hpp, which just puts extern "C" around their own header files for you, suppressing the name mangling and making the linker all happy again.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this