Lua无法加载库

3
我决定使用Lua进行脚本编写。我已经下载并编译了解释器。它可以正常工作,但是当我想使用任何来自os.*或string.*库的函数时,它会显示“attemt to index global 'os' (a nil value)”。

以下是我的代码,应该可以工作,但事实并非如此:

#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;
}

这是什么问题?
1个回答

5
在Lua 5.2中,标准的luaopen_*函数不会设置相应的全局变量。
为什么不复制并调整linit.c中的代码或者直接调用luaL_openlibs呢?
否则,请按照它们的方式:为每个luaopen_*函数调用luaL_requiref
请参见http://www.lua.org/source/5.2/linit.c.html#luaL_openlibs

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接