看了网上前人的碎片, 也贴个自己手写调试过的小示例.
环境: Ubuntu, Eclipse, lua5.1, gcc-4.6
两个文件: hello.cpp, test.lua
hello.cpp 读取 test.lua的配置内容:
/*
* hello.cpp
*
* Created on: Jul 16, 2012
* Author: honghe
*/
#include <iostream>
#include <cstdio>
#include <lua.hpp>
using namespace std;
struct info{
string name;
int age;
};
typedef struct info info;
int main(int argc, char **argv) {
lua_State* L = luaL_newstate();
info employee;
luaL_dofile(L,"./test.lua");
lua_getglobal(L,"name");
lua_getglobal(L,"age");
employee.name = lua_tostring(L,-2);
employee.age = lua_tonumber(L,-1);
cout << employee.name << endl;
cout << employee.age << endl;
getchar();
return 0;
}
--test.lua
name = "ubuntu"
age = 12.04
参考:http://demon.tw/programming/embedded-lua-in-c.html