|
发表于 2009-7-3 19:38:08
|
显示全部楼层
lua调用c
编译为dll,放在 mush下:
#include "stdio.h"
#include "lua.hpp"
static int l_test(lua_State *L) {
double d = luaL_checknumber(L, 1); // 获取传参
lua_pushnumber(L, d * 0.1); // 返回值 = 参数/10
return 1;
};
static const struct luaL_Reg testlib [] = {
{"test", l_test},
{NULL, NULL} // 表明结尾
};
extern "C"{
__declspec(dllexport)
int luaopen_test(lua_State *L)
{
luaL_register(L, "testlib", testlib);
return 1;
}
}
---
lua调用:
f = package.loadlib ("testlib/testlib.dll", "luaopen_test")
assert (f ())
n = assert (testlib.test (3.14))
Note ("result=", n)
输出:
|
|