5
一個非常基本的問題,我想:luabind:無法調用諸如打印基本盧阿功能的toString
的C++代碼,調用LUA看起來是這樣的:
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);
現在test.lua有下列內容:
function main()
print "1"
end
在執行時收到錯誤:
test.lua:2: attempt to call global 'print' (a nil value)
問題是什麼?它與環境有關嗎?我認爲像print這樣的功能是在全球環境中定義的。爲什麼沒有找到呢?
非常感謝。
好吧,我想通了:luaopen_base (M_L);需要在lua_open() – 2012-02-24 13:17:59
之後調用,如果您打算使用這些庫,則需要調用luaopen_string(m_L),luaopen_math(m_L)等來打開相應的內置lua庫。 – 2012-02-24 15:41:56
@ chris.schuette:您應該回答自己的問題並接受該答案(從而將問題標記爲正在完成)或刪除問題。 – 2012-02-24 16:48:04