
怎么在GCC中lua与c函数互相调用
想写一个游戏,服务器端想用lua定义技能最简单的:C:intechohello(){printf('hello');return1;}接下来如何用lua调用它(lua.c...
想写一个游戏,服务器端想用lua定义技能
最简单的:
C:
int echohello()
{
printf('hello');
return 1;
}
接下来如何用lua调用它
(lua.c等文件已经有了)
注意是用同一个c程序调用(接着前面的c代码)。不是直接运行lua 展开
最简单的:
C:
int echohello()
{
printf('hello');
return 1;
}
接下来如何用lua调用它
(lua.c等文件已经有了)
注意是用同一个c程序调用(接着前面的c代码)。不是直接运行lua 展开
1个回答
展开全部
#include <stdio.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
int echohello()
{
printf("hello");
//return 1;
return 0;
}
int main()
{
lua_State* ls = luaL_newstate();
luaL_openlibs(ls);
lua_pushcclosure(ls, echohello, 0);
lua_setglobal(ls, "echohello");
luaL_dostring(ls,
"print('start call c function...')\r\n"
"echohello()\r\n"
"print('')"
"print('end call c function...')\r\n"
);
lua_close(ls);
return 0;
}
我假设你知道如何设置lua编译环境,如何链接lua库
我假设你使用的是c语言,并且知道函数调用的内部原理(不知道的话请不要像我这样把echohello给pushcclosure进去)
gcc下是一样的。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询