【问题标题】:trying to build and compile lua 5.3.3 with mingw , undefined reference error尝试使用 mingw 构建和编译 lua 5.3.3,未定义的引用错误
【发布时间】:2017-06-05 14:55:47
【问题描述】:

所以基本上我得到了 lua5.3.3 源代码,我正在尝试用 mingw 构建它

到目前为止,我所做的是我在 msys 中完成了整个操作,然后复制了 lib 文件和 bin 文件并将文件包含到 mingw 适当的文件夹中

但是,当我尝试实际编译使用它的应用程序时,我得到了这些错误

这是我用来编译使用 lua 的程序的命令

gcc syx.cpp -llua

C:\Users\User\AppData\Local\Temp\cckJPF8N.o:syx.cpp:(.text+0xf): undefined reference to `luaL_newstate()'
C:\Users\User\AppData\Local\Temp\cckJPF8N.o:syx.cpp:(.text+0x21): undefined reference to `luaL_openlibs(lua_State*)'
C:\Users\User\AppData\Local\Temp\cckJPF8N.o:syx.cpp:(.text+0x3e): undefined reference to `luaL_loadfilex(lua_State*, char const*, char const*)'
C:\Users\User\AppData\Local\Temp\cckJPF8N.o:syx.cpp:(.text+0x77): undefined reference to `lua_pcallk(lua_State*, int, int, int, int, int (*)(lua_State*, int, int))'
C:\Users\User\AppData\Local\Temp\cckJPF8N.o:syx.cpp:(.text+0x87): undefined reference to `lua_close(lua_State*)'
collect2.exe: error: ld returned 1 exit status

这是文件(非常基本的),以防您需要查看它

#include <stdio.h>

#include <lua5.3/lua.h>
#include <lua5.3/lualib.h>
#include <lua5.3/lauxlib.h>

/* the Lua interpreter */
lua_State* L;

int main ( int argc, char *argv[] )
{
   L = luaL_newstate();

   luaL_openlibs(L);

   luaL_dofile(L, "test.lua");

   /* cleanup Lua */
   lua_close(L);

        return 0;
}

我知道库文件存在,因为它是使用 mingw 创建的,即位于我的 mingw lib 文件夹中的 liblua.a,以及与 lua 相关的其他文件,例如 lua.exe luac.exe 包含文件等,所以我我不确定还缺少什么

【问题讨论】:

  • 请改用#include "lua.hpp"。它基本上做了你在下面做的事情。如果您正在编译 c++ 代码,也请使用 g++。如果你真的想编译 C,那么不要使用 .cpp 扩展名,使用 .c。

标签: c windows lua


【解决方案1】:

好的,我找到了解决方案

原来 gcc 正在修改库头文件中的符号(不管这意味着什么)

需要做的是需要将标头包装在 extern "C" {//headers} 中以使其工作

参考在这里:

http://www.linuxquestions.org/questions/programming-9/undefined-reference-error-when-using-lua-api-892782/

为了清楚起见,这是一个工作示例:

#include <stdio.h>

extern "C"{
#include <lua5.3/lua.h>
#include <lua5.3/lualib.h>
#include <lua5.3/lauxlib.h>
}

/* the Lua interpreter */
lua_State* L;

int main ( int argc, char *argv[] )
{
   L = luaL_newstate();

   luaL_openlibs(L);

   luaL_dofile(L, "test.lua");

   /* cleanup Lua */
   lua_close(L);

        return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多