Lua教程(十九):C调用Lua
1.基础:
Lua的一项重要用途就是作为一种配置语言。现在从一个简单的示例开始吧。
--这里是用Lua代码定义的窗口大小的配置信息 width=200 height=300
下面是读取配置信息的C/C++代码:
#include<stdio.h> #include<string.h> #include<lua.hpp> #include<lauxlib.h> #include<lualib.h>
voidload(lua_State*L,constchar*fname,int*w,int*h){ if(luaL_loadfile(L,fname)||lua_pcall(L,0,0,0)){ printf("ErrorMsgis%s.\n",lua_tostring(L,-1)); return; } lua_getglobal(L,"width"); lua_getglobal(L,"height"); if(!lua_isnumber(L,-2)){ printf("'width'shouldbeanumber\n"); return; } if(!lua_isnumber(L,-1)){ printf("'height'shouldbeanumber\n"); return; } *w=lua_tointeger(L,-2); *h=lua_tointeger(L,-1); }
intmain() { lua_State*L=luaL_newstate(); intw,h; load(L,"D:/test.lua",&w,&h); printf("width=%d,height=%d\n",w,h); lua_close(L); return0; }
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下