Lua 5.3 未定义的引用

2021-12-30 00:00:00 lua c++

我正在尝试学习如何将 lua 嵌入到 C 程序中,但我不擅长阅读技术文档,而且我还没有找到任何当前的教程.这是我的程序:

I am trying to learn how to embed lua in a C program, but I am not great at reading technical documents, and I haven't found any current tutorials. This is my program:

#include <iostream>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>


void report_errors(lua_State*, int);

int main(int argc, char** argv) {
    for (int n = 1; n < argc; ++n) {
        const char* file = argv[n];
        lua_State *L = luaL_newstate();

        luaL_openlibs(L);

        std::cerr << "-- Loading File: " << file << std::endl;

        int s = luaL_loadfile(L, file);

        if (s == 0) {
            s = lua_pcall(L, 0, LUA_MULTRET, 0);
        }

        report_errors(L, s);
        lua_close(L);
        std::cerr << std::endl;
    }
    return 0;
}

void report_errors(lua_State *L, int status) {
    if (status) {
        std::cerr << "-- " << lua_tostring(L, -1) << std::endl;
        lua_pop(L, 1);
    }
}

编译器给出了 luaL_newstate、luaL_openlibs、luaL_loadfilex、lua_pcallk 和 lua_close 的未定义引用错误.我在 Windows 计算机上使用 Code::Blocks one,并且已将 lua 包含目录添加到所有搜索路径,并将 liblua53.a 添加到链接库.IDE 自动完成头名称,解析器显示大部分 lua 函数,但通过简短的搜索,我发现解析器找不到 lua_newstate 或 luaL_newstate.为什么它会找到某些功能而不是其他功能?

The compiler gives undefined reference errors for luaL_newstate, luaL_openlibs, luaL_loadfilex, lua_pcallk, and lua_close. I am using Code::Blocks one a Windows computer and I have added the lua include directory to all of the search paths and liblua53.a to the link libraries. The IDE autocompleted the header names and the parser displays most of the lua functions, but with a brief search I found that the parser could not find either lua_newstate or luaL_newstate. Why does it find some of the functions and not others?

推荐答案

g++ 的参数在输入文件之前有 -llua.我把 -llua 放在最后,现在一切正常.

The arguments for g++ had -llua before the input file. I put -llua at the end, and everything works fine now.

相关文章