我有两个文件 - 一个用于执行 Lua 脚本和脚本本身。
他们来了:
主机.cpp:
#include <lua.hpp>
#include <iostream>
using namespace std;
int someHandler(lua_State *l)
{
int argc = lua_gettop(l);
for (int i = 0; i < argc; i++)
{
cout << "ARG[" << i + 1 << "] = " << lua_tostring(l, i + 1) << endl;
}
lua_pushstring(l, "m_pi");
//lua_pop(l, argc - 1);
//lua_pushnumber(l, 3.14);
return argc;
}
int main()
{
lua_State *l = lua_open();
luaL_openlibs(l);
lua_register(l, "moo", someHandler);
luaL_dofile(l, "script.lua");
lua_close(l);
return 0;
}
脚本.lua:
res = moo("hello", "world");
print(moo());
for k, v in res do
print(k.." = "..v);
end
用. host.cpp
_g++ host.cpp -o host.elf -I/usr/include/lua5.1 -llua5.1
运行的结果host.elf
是:
ARG[1] = hello
ARG[2] = world
<\n>
虽然它应该是:
ARG[1] = hello
ARG[2] = world
m_pi
我做错了什么?