怎么在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
展开
 我来答
空雪梦见
推荐于2016-05-31 · TA获得超过5598个赞
知道大有可为答主
回答量:2522
采纳率:75%
帮助的人:1293万
展开全部
#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下是一样的。

推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式