Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Undefined reference to" using Lua

Tags:

c++

reference

lua

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.

like image 869
M Darblade Avatar asked Dec 27 '12 19:12

M Darblade


2 Answers

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>
}
like image 58
Javier Mr Avatar answered Nov 05 '22 23:11

Javier Mr


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.

like image 40
T Suds Avatar answered Nov 06 '22 01:11

T Suds