5

A very basic question i guess:

The C++ code, calling lua looks like this:

lua_State* m_L;
m_L = lua_open();
luabind::open(m_L);
luaL_dofile(m_L, "test.lua");
try {
    luabind::call_function<void>(m_L, "main");
} catch (luabind::error& e) {
    std::string error = lua_tostring(e.state(), -1);
    std::cout << error << std::endl;
}
lua_close(m_L);

now test.lua has the following contents:

function main()
print "1"
end

Upon execution I receive the error:

test.lua:2: attempt to call global 'print' (a nil value)

What is the problem? It has something to do with environments? I thought functions like print are defined in the global environment. Why is it not found then?

Thank you very much.

4

1 回答 1

6

如您所见,您必须调用luaopen_basegetprint和其他基本功能。然后你需要调用luaopen_string, luaopen_math, 来获取基本模块和函数。而不是手动写出来,可以一次加载所有 Lua 基本函数luaL_openlibs

lua_State* m_L = luaL_newstate();
luaL_openlibs(m_L);
于 2012-02-25T17:34:03.800 回答