luaはc/c++プロファイルの例として

1060 ワード

ネット上の前人の破片を見て、自分で手書きでデバッグした例も貼った.環境:Ubuntu,Eclipse,lua 5.1, gcc-4.6
2つのファイル: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