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.
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>
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With