3

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

どうしたの?

4

1 に答える 1

5

Lua 5.2 では、標準luaopen_*関数は対応するグローバル変数を設定しません。

のコードをコピーして適応させるlinit.cか、単に呼び出してみluaL_openlibsませんか?

luaL_requirefそれ以外の場合は、関数ごとに呼び出しluaopen_*ます。

http://www.lua.org/source/5.2/linit.c.html#luaL_openlibsを参照してください。

于 2012-05-30T18:05:21.823 に答える