怎么在GCC中lua与c函数互相调用

想写一个游戏,服务器端想用lua定义技能
最简单的:
C:
int echohello()
{
printf('hello');
return 1;
}
接下来如何用lua调用它
(lua.c等文件已经有了)
注意是用同一个c程序调用(接着前面的c代码)。不是直接运行lua

#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下是一样的。

温馨提示:答案为网友推荐,仅供参考
相似回答