【发布时间】:2015-11-18 20:01:43
【问题描述】:
int init_lua (char *filename,lua_State *L){
//int inter;
//lua_State *L = luaL_newstate();
luaL_openlibs(L);
if(luaL_loadfile(L, filename) ){
error(L, "cannot run configuration file: %s",lua_tostring(L, -1));
return 1;
}
if (lua_pcall(L, 0, 0, 0)){
error(L, "lua_pcall() failed");
return 2;
}
return 0;
}
uint8_t get_interface(unsigned int inteiro,lua_State *L){
int inter;
lua_getglobal(L,"ret_ind"); //função a ser chamada
lua_pushnumber(L,inteiro); //argumento
// do the call (1 arguments, 1 result)
if (lua_pcall(L, 1, 1, 0))
error(L, "error running function 'f': %s",lua_tostring(L, -1));
if (!lua_isnumber(L, -1))
error(L, "function 'f' must return a number");
inter = lua_tonumber(L, -1);
lua_pop(L, 1); // pop returned value /
return (uint8_t)inter;
}
int main(void)
{
lua_State *L = luaL_newstate();
printf("apos o load3\n");
if (init_lua("pp.lua",L))
printf("erro ao tentar inicializar arquivo lua\n");
uint8_t resul;
resul = get_interface(190,L);
printf("Index of array: %d\n",(int)resul);
resul = get_interface(10000,L);
printf("Index of array: %d\n",(int)resul);
return 0;
}
以上代码为 pp.c
interfaces = {190,3141592,10000}
function ret_ind (ip)
for i, inter in ipairs(interfaces) do
if inter == ip then
return i
end
end
return 0;
end
以上代码为pp.lua
和编译命令 gcc -o pp pp.c -I/usr/include/lua5.1/ -llua5.1
我正在尝试包含 lua 的源代码,因此我不需要在机器上安装 lua。从系统中删除 liblua5.1-0-dev 后,我无法编译以下内容。
gcc -std=c99 -O2 -I./lua5.1/src/pp.c -o pp.o -llua5.1
通过系统上的库,我可以使用本地源代码,但可以使用来自 -llua5.1 的全局 lua5.1。
编辑
将 lua 源代码转换为 shered 对象 (.so) 后,我得到了一个执行程序: OBS:我会将我使用的makefile作为这篇文章的评论
gcc -o pp pp.c -I./lua5.1/src/ -L./lua5.1/src/ -llua5.1 执行我使用的程序 (必须执行 2 follow 命令才能让 ld 找到 .so 文件)
LD_LIBRARY_PATH=./lua5.1/src/ 导出 LD_LIBRARY_PATH 并运行 ./pp
【问题讨论】:
-
这是一个工作程序。
-
您遇到什么错误?为什么不静态链接 Lua 而不是将源代码移植到你的项目中?