【问题标题】:Lua 5.2: undefined symbols when using luaL_dofile()Lua 5.2:使用 luaL_dofile() 时未定义的符号
【发布时间】:2013-12-18 22:26:27
【问题描述】:

我尝试使用以下 C++ 代码进行一些简单的 Lua 5.2 嵌入:

void dnLuaRunner::Exec(string file)
{
    // Initialize the lua interpreter.
    lua_State * L;
    L = luaL_newstate();

    // Load lua libraries.
    static const luaL_Reg luaLibs[] =
    {
        {"math", luaopen_math},
        {"base", luaopen_base},
        {NULL, NULL}
    };

    // Loop through all the functions in the array of library functions
    // and load them into the interpreter instance.
    const luaL_Reg * lib = luaLibs;
    for (; lib->func != NULL; lib++)
    {
        lib->func(L);
        lua_settop(L, 0);
    }

    // Run the file through the interpreter instance.
    luaL_dofile(L, file.c_str());

    // Free memory allocated by the interpreter instance.
    lua_close(L);
}

第一部分是一些基本的初始化和加载一些标准库模块的代码,但是当我调用luaL_dofile(...) 时,它似乎引发了一些未定义符号的错误。 luaL_dofile 是一个宏,它使用像 luaL_loadfilelua_pcall 这样的函数,所以我收到以下链接器错误看起来并不可怕:

  "_luaL_loadfilex", referenced from:
      dnLuaRunner::Exec(std::string) in dnLuaRunner.cc.o
  "_lua_pcallk", referenced from:
      dnLuaRunner::Exec(std::string) in dnLuaRunner.cc.o
ld: symbol(s) not found for architecture x86_64

我在 Makefile 中正确链接了 liblua.a

【问题讨论】:

    标签: c++ c compiler-construction linker lua


    【解决方案1】:

    原来你需要添加-lm-llua 并且它们都必须在你要编译的文件之后,像这样:

    # or clang++ works too
    $ g++ ... foofy.c -llua -lm
    

    我还看到了你必须在最后使用-ldl 的情况。

    【讨论】:

    • 我记得,-ldl 也是必需的。链接器选项,没什么特别的。
    • @Kamiccolo 我也试过 -llua-ldl 但只是 -llua-lm 对我很有效。
    • @beakr -lm 用于math.h 头文件,对吗?有人告诉我,如果我在 .c 文件中使用了math.h 函数,则在编译时需要包含-lm。但似乎我在用 gcc 编译时不必包含它。那么,它是特定于编译器的吗?
    • @YickLeung 我很确定math.h 是标准库的一部分,不需要链接。我是从用g++clang++ 或类似的东西编译的 C++ 的角度写的。 编辑:我认为-lm 是由 AMD 开发的数学函数库,不同于标准的math.h
    猜你喜欢
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    • 2015-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-28
    • 1970-01-01
    相关资源
    最近更新 更多