Jump to content
  • Advertisement
Sign in to follow this  
Googol PL3X

luaL_openlibs(L) Linking problems

This topic is 3635 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
It may be because you are including the raw C header files. Try including "lua.hpp" instead, this will add a extern "C" { } to your includes, and should solve a few problems.

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
A lot of work; lua is a C library. The use of the lua.hpp header works perfectly fine, there isn't really any reason to change lua to a C++ library at all.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!