link error with “undefined lua_xxxxx” when buildin

2019-09-14 21:10发布

问题:

In my ubuntu 14.xx, I try to compile lsnes emulator to use the mario-ai script from aleju/mario-ai, and I've tried to google many solutions to solve the problem below:

Here is the output from the console:

make[3]: __all__.files' is up to date. make[3]: Leaving directory/home/pengsuyu/software/lsnes/sourcecode/src/platform/macosx' make[2]: Leaving directory /home/pengsuyu/software/lsnes/sourcecode/src/platform' g++ -o lsnescat all_common.files all_platform.files-pthread -lboost_iostreams -lboost_filesystem -lboost_system -lz -lgcrypt -lgpg-error -L/usr/lib/x86_64-linux-gnu -lcurl -rdynamic -ldlcat core/all.ldflags lua/all.ldflags fonts/all.ldflags library/all.ldflags interface/all.ldflags video/all.ldflags emulation/all.ldflags cmdhelp/all.ldflags platform/all.ldflags core/multitrack.o: In functionlua::state::get_string(int, std::string const&)': /home/pengsuyu/software/lsnes/sourcecode/src/core/../../include/library/lua-base.hpp:317: undefined reference to lua_tolstring' core/multitrack.o: In functionlua::state::get_bool(int, std::string const&)': /home/pengsuyu/software/lsnes/sourcecode/src/core/../../include/library/lua-base.hpp:334: undefined reference to lua_toboolean' core/multitrack.o: In functionlua::state::type(int)': . . /home/pengsuyu/software/lsnes/sourcecode/src/library/lua.cpp:536: undefined reference to lua_close' library/lua.o: In functionlua::state::pushcfunction(int ()(lua_State))': /home/pengsuyu/software/lsnes/sourcecode/src/library/../../include/library/lua-base.hpp:504: undefined reference to lua_pushcclosure' library/lua.o: In functionlua::state::getfield(int, char const*)': /home/pengsuyu/software/lsnes/sourcecode/src/library/../../include/library/lua-base.hpp:506: undefined reference to lua_getfield' library/lua.o: In functionlua::state::insert(int)': /home/pengsuyu/software/lsnes/sourcecode/src/library/../../include/library/lua-base.hpp:509: undefined reference to lua_insert' collect2: error: ld returned 1 exit status make[1]: *** [lsnes] Error 1 make[1]: Leaving directory/home/pengsuyu/software/lsnes/sourcecode/src' make: *** [src/all_files] Error 2

==================================

At the beginning, I think, the linker cannot find my lua library. So I tried to compile my main.cpp with test.lua.

main.cpp:

#include <stdio.h>
#include <iostream>

//extern "C"
//{
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
//} // liblua5.1-c++.a 
lua_State * L;
int main ()
{
     L = lua_open();
     luaL_openlibs(L);
     luaL_dofile(L, "d:\\test.lua");
     return 0;
}

test.lua:

print("Hello World");

I write a MakeFile to generate the executable file "main":

main:main.o
    gcc -o $@ $< -llua5.1 -lstdc++
main.o:
    gcc -c main.cpp
clean:
    -rm *.o

It works when I add the compile option "-llua5.1" and "-lstdc++" otherwise it throws the same error as I compiled lsnes

I am not familiar with gcc and Makefile. Please help me to solve this problem.

回答1:

I've solved my question

The way to solve this problem is just to change one line in the file named "options.build".

1. find the line "LUA=lua" in options.build

2. change this line to "LUA=lua5.1"

because the needed library is 5.1, so if you want to build it successfully, you must use the "lua5.1" library however the default configuration is "lua" not "lua5.1"