C++向lua传递全局变量

lua_pushstring(L,"c name value");

lua_setglobal(L,"c name");//将栈顶位置设置全局变量并出栈

 

例子:

C++程序

int main()
{
	lua_State *L = luaL_newstate();
	if (L == NULL)
	{
		return 1;
	}
	luaL_openlibs(L);

	lua_pushstring(L, "hello");
	lua_setglobal(L, "test");

	int ret = luaL_dofile(L, "main.lua");
	if (ret)
	{
		printf("Lua doFile Error !\n");
	}
}

lua脚本:

print("c++ test = "..test)

运行结果:

 

你可能感兴趣的:(C++)