gpt4 book ai didi

Lua 不加载库

转载 作者:行者123 更新时间:2023-12-01 11:51:49 25 4
gpt4 key购买 nike

我决定用 Lua 添加脚本。我已经下载并编译了解释器。它工作正常,但是当我想使用 os.* 或 string.* 库中的任何函数时,它说,“尝试索引全局‘os’(一个 nil 值)”

这是我的代码,应该可以工作,但没有:

#include <iostream>
#include <Windows.h>
#include <string>

using namespace std;

extern "C" {
#include "..\liblua\lua.h"
#include "..\liblua\lualib.h"
#include "..\liblua\lauxlib.h"
}


int main(int argc, TCHAR* argv[])
{
lua_State *LuaVM = luaL_newstate();

lua_pushcfunction(LuaVM,luaopen_base);
lua_call(LuaVM,0,0);
lua_pushcfunction(LuaVM,luaopen_math);
lua_call(LuaVM,0,0);
lua_pushcfunction(LuaVM,luaopen_string);
lua_call(LuaVM,0,0);
lua_pushcfunction(LuaVM,luaopen_table);
lua_call(LuaVM,0,0);

int error;
lua_pushstring(LuaVM,"Ver 0.525.5");
lua_setglobal(LuaVM,"Version");

while (true)
{
string strCode;
getline(cin,strCode);
error = luaL_loadbuffer(LuaVM,strCode.c_str(),strCode.length(),"") ||
lua_pcall(LuaVM,0,0,0);
if (error)
{
cout<< lua_tostring(LuaVM,-1)<<endl;
lua_pop(LuaVM,1);
}
}

lua_close(LuaVM);

return 0;
}

这是怎么回事?

最佳答案

在 Lua 5.2 中,标准的 luaopen_* 函数设置相应的全局变量。

为什么不复制和修改 linit.c 中的代码或直接调用 luaL_openlibs

否则,做他们所做的:为每个 luaopen_* 函数调用 luaL_requiref

参见 http://www.lua.org/source/5.2/linit.c.html#luaL_openlibs .

关于Lua 不加载库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10821316/

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