Linux中如何在C语言中嵌入Lua

Linux中如何在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下是一样的。

这个问提之前我已经回答过了,先上一张我厂大拿分享的知识:

gcc -o hello hello.c -I /usr/include/lua5.1/

编译成可执行文件,-I后面是我linux上库依赖的头文件地址

楼主可参考下,望对你有所帮助!


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/6148318.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-16
下一篇 2023-03-16

发表评论

登录后才能评论

评论列表(0条)

保存