gpt4 book ai didi

c++ - 在 C++ 中嵌入 lua 的问题

转载 作者:搜寻专家 更新时间:2023-10-31 00:18:02 25 4
gpt4 key购买 nike

我试图在 C++ 中嵌入 lua 代码,但我遇到了一个奇怪的编译器错误。这是我的代码:

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

int main() {
lua_State *luaVM = luaL_newstate();
if (luaVM == NULL) {
printf("Error initializing lua!\n");
return -1;
}

luaL_openlibs(luaVM);

luaL_dofile(luaVM, "test.lua");

lua_close(luaVM);

return 0;
}

编译:

g++ -Wall -o embed -llua embed.cpp

错误是:

g++ -Wall -o embed -llua embed.cpp
/tmp/ccMGuzal.o: In function `main':
embed.cpp:(.text+0x47): undefined reference to `luaL_loadfilex'
embed.cpp:(.text+0x72): undefined reference to `lua_pcallk'
collect2: error: ld returned 1 exit status

我没有从我的代码中调用 luaL_loadfilexlua_pcallk,这有助于假设问题不在我的代码中,而在 lua 本身。这里有人有什么想法吗?

更新

这是我的版本信息:

$ lua -v
Lua 5.2.0 Copyright (C) 1994-2011 Lua.org, PUC-Rio

最佳答案

在 lua 5.2.1 中 luaL_dofile 是一个声明如下的宏:

#define luaL_dofile(L, fn) \
(luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0))

在你的 lua 版本中,它很可能用 luaL_loadfilexlua_pcallk 实现,你会得到 undefined reference ,就像@Shahbaz 说的那样。

关于c++ - 在 C++ 中嵌入 lua 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11742505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com