“Undefined reference to” using Lua

不羁的心 提交于 2020-01-01 10:46:07

问题


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.


回答1:


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>
}



回答2:


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.



来源:https://stackoverflow.com/questions/14060009/undefined-reference-to-using-lua

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!