c++ - 链接器找不到 Lua 库定义

标签 c++ linux lua g++ linker-errors

所以,我是 C++ 的新手,我一直在尝试在我的 C++ 项目中运行 Lua 文件。首先,我运行了这个简单的代码:

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

int main() {
    std::cout << "Lua Console";

    lua_State *L;
    L = luaL_newstate();

    char ch;
    std::cin >> ch;
    return 0;
}

终端命令如下所示:

g++ main.cpp -L lib/liblua.a

编辑:更正此

g++ main.cpp -Llib -llua

Lua 库安装在我的系统 (Linux) 上,但链接器找不到 luaL_newstate() 的定义,尽管我在命令行中包含了该库:

main.cpp:(.text+0x2b): undefined reference to `luaL_newstate()'

任何 Lua 函数都会发生这种情况。我真的不知道出了什么问题,到目前为止,在 Linux 中导入库非常简单。这似乎是我不熟悉在我的项目中使用库的问题,但我找不到非常有用的文档。提前谢谢你。

最佳答案

-L 选项指定存放库文件的文件夹;试试 g++ main.cpp -Llib -llua

同时将 Lua header 的 #include 包装到 extern "C" 中:

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

关于c++ - 链接器找不到 Lua 库定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57967479/

相关文章:

c++ - 套接字 - 在数据传输后保持套接字打开

string - 按端口号对 IPv4 地址进行排序

web - 从网页执行Lua

c++ - 获取作为类变量的双端队列大小时的未定义行为

c++ - 视频对象链表插入函数

Python 守护进程线程在中断 sys.exit() 时清理逻辑

c++ - 处理客户端连接的最有效方式(套接字编程)

c++ - (C++) 堆上的内存变得可访问/被删除

c++ - 使用 TLS 的缓存未命中率更低?

从 Lua 通过 Swig 调用 C 函数指针