与从源代码编译的 Lua 链接时未定义的引用?

Undefined references when linking with Lua compiled from source?

使用 TDM-GCC 4.5.1 从源代码下载 lua5_1_4_Sources.tar.gz 并编译 liblua.a。

编译一切正常:

Linking ..
  ar rcs liblua.a 5.1.4/src/lapi.o 5.1.4/src/lauxlib.o 5.1.4/src/lbaselib.o 5.1.4/src/lcode.o 5.1.4/src/ldblib.o 5.1.4/s rc/ldebug.o 5.1.4/src/ldo.o 5.1.4/src/ldump.o 5.1.4/src/lfunc.o 5.1.4/src/lgc.o 5.1.4/src/linit.o 5.1.4/src/liolib.o 5.1 .4/src/llex.o 5.1.4/src/lmathlib.o 5.1.4/src/lmem.o 5.1.4/src/loadlib.o 5.1.4/src/lobject.o 5.1.4/src/lopcodes.o 5.1.4/s rc/loslib.o 5.1.4/src/lparser.o 5.1.4/src/lstate.o 5.1.4/src/lstring.o 5.1.4/src/lstrlib.o 5.1.4/src/ltable.o 5.1.4/src/ ltablib.o 5.1.4/src/ltm.o 5.1.4/src/lua.o 5.1.4/src/luac.o 5.1.4/src/lundump.o 5.1.4/src/lvm.o 5.1.4/src/lzio.o 5.1.4/sr c/print.o 5.1.4/src/wmain.o

编译示例源就好了: http://pastebin.com/EGvMRjth

尝试过 link:

Compiling..
  g++ test.o -g -shared -Lshared/build/lua -llua -o libtest.so
test.o:test.cpp:(.text+0x15): undefined reference to `lua_tonumber(lua_State*, int)'
test.o:test.cpp:(.text+0x40): undefined reference to `lua_pushnumber(lua_State*, double)'
test.o:test.cpp:(.text+0x60): undefined reference to `lua_tonumber(lua_State*, int)'
test.o:test.cpp:(.text+0x8e): undefined reference to `lua_pushnumber(lua_State*, double)'
test.o:test.cpp:(.text+0xb6): undefined reference to `lua_pushcclosure(lua_State*, int (*)(lua_State*), int)'
test.o:test.cpp:(.text+0xd1): undefined reference to `lua_setfield(lua_State*, int, char const*)'
test.o:test.cpp:(.text+0xec): undefined reference to `lua_pushcclosure(lua_State*, int (*)(lua_State*), int)'
test.o:test.cpp:(.text+0x107): undefined reference to `lua_setfield(lua_State*, int, char const*)'
collect2: ld returned 1 exit status

我正在尝试按照此处的示例进行操作:

http://www.troubleshooters.com/codecorn/lua/lua_lua_calls_c.htm

正如评论中已经讨论的那样.. 看来您正在编译 C++,并且您使用的 Lua 版本可能是用 C 编码的。 这意味着您需要包含 lua.hpp 或者如果不可用,您需要使用 extern "C" 来包含库,如下所示:

extern "C" {
    #include "lua.h"
    #include "lualib.h"
    #include "lauxlib.h"
};