15

I got the error undefined reference to 'luaL_newstate' when I try to build my project. I know it's an error with the linker but I'm relatively new to Lua and adding a library to a project. I use Code::Blocks by the way. API functions luaL_openlibs, luaL_loadfile, lua_pcall, lua_getfield, lua_type, lua_settop are missing too.

I saw on a webite that I have to link my project with libdl in order to solve this problem, but I don't really know what it mean and how to do it.

prapin
  • 6,395
  • 5
  • 26
  • 44
M Darblade
  • 323
  • 1
  • 4
  • 13
  • Is `luaL_newstate` the only symbol missing? Are you using other `lua*` API functions in your project? – prapin Dec 27 '12 at 20:05
  • @prapin `luaL_newstate` `luaL_openlibs` `luaL_loadfile` `lua_pcall` `lua_getfield` `lua_type` `lua_settop ` I have these missing reference – M Darblade Dec 27 '12 at 20:12
  • So you are not linking with Lua library, or with an incompatible format of the library. Did you specify `-l lua`? – prapin Dec 27 '12 at 20:18
  • @prapin I tried specifying `-l lua` in Project->Build option-> Other Options and I typed `-l lua` but I have the same error – M Darblade Dec 27 '12 at 20:28
  • To go further, I think I will now need the complete command line to run the linker and the output produced by the linker. Consider posting to Pastebin if the content is too big. – prapin Dec 27 '12 at 20:37
  • Please add the following information to your question: what compiler do you use? What is the exact error message? Please also give the name of the IDE if you use one. If possible add the compiler command line, otherwise provide all related project options. – Zeta Dec 27 '12 at 20:38
  • @prapin I've downloaded again the lib and define so I've a new error. I now have the error `ld.exe||cannot find -llua52|` – M Darblade Dec 27 '12 at 21:16
  • @Zeta I use gcc and Code::Blocks 10.05. How can i get the compiler command line on Code::Blocks? – M Darblade Dec 27 '12 at 21:17
  • @Zeta Install Lua? I've succesfully compiled a Visual C++ project using lua on this computer so I think it's installed – M Darblade Dec 27 '12 at 21:52
  • So you have compiled Lua with Visual C++ and you compile your project with GCC? This cannot work, you need the same compiler. – prapin Dec 28 '12 at 09:17

3 Answers3

25

I faced the same problem, in my case I found a solution that worked for me here. Basically consist in wrapping the #include s of lua inside a extern "C", like:

extern "C"{
    #include <lua5.2/lualib.h>
    #include <lua5.2/lauxlib.h>
    #include <lua5.2/lua.h>
}
Javier Mr
  • 2,130
  • 4
  • 31
  • 39
3

Lua can be a bit complex when you're first trying to compile it. The website that you referenced was correct: libdl is pretty much required when linking Lua.

I don't have Code::Blocks in front of me, so I can't really tell you what options you need to add. It should be a list of "command line options" or "compiler options". If you were compiling from the command line, the full command would look like:

gcc -o sample sample.c -llua -ldl

Note that the -l options have no space before the library name. There should be an option in Code::Blocks to add your own compile-time options or compiler flags. You would add "-llua" and "-ldl" to that options list. Alternatively, just do it yourself from the command line.

libdl is a library that is used when dynamically linking other libraries into your program. You have to add it in for Lua to be linked correctly.

T Suds
  • 317
  • 2
  • 7
0

As the accepted answer mentioned, these headers lack of extern "C". An alternative is to include the installed lua.hpp, which already use extern "C" to wrap these Lua headers.

// lua.hpp
// Lua header files for C++
// <<extern "C">> not supplied automatically because Lua also compiles as C++

extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
for_stack
  • 21,012
  • 4
  • 35
  • 48