test.c #include <stdlib.h>
#include <stdio.h>
#include <string.h>
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
};
int main() {
lua_State *L = luaL_newstate();
luaL_openlibs(L);
if(luaL_loadfile(L, "text.lua") || lua_pcall(L, 0,0,0)){
printf("error123 %s\n", lua_tostring(L,-1));
return -1;
}
lua_getglobal(L,"width");
lua_getglobal(L,"height");
printf("width = %d\n", lua_tointeger(L,-2));
printf("height = %d\n", lua_tointeger(L,-1));
lua_close(L);
return 0;
}
lua
width = 10
height = 20 运行:
g++ -o test test.c -ldl liblua.a
./test
本文展示了一个使用C语言调用Lua脚本的例子,通过加载Lua脚本并获取其中定义的变量值,演示了C与Lua之间的基本交互过程。
6150

被折叠的 条评论
为什么被折叠?



