C/C++调用lua编程开发
1. C/C++ 调用lua 有两种方式
- 通过库调用,即将lua编译出动态库或者静态库;然后开发中只需要添加头文件进行编译即可实现。 网络此方式使用的列子很多,在单片机开发中使用这种方式不方便。
- 通过源代码编译,直接将lua源码编译的方式。 (没找到专业的名词,自行理解) 使用此方式确实是想将lua应用到单片机中,当用户有需求改动时,也可以通过升级lua脚本的方式实现定制化。
2. C/C++调用lua源代码
- 下载源码回来,直接将lua.c 和 lua.c 删除;这两个文件有两个main编译.o文件时候不调用不方便 。也可以手动修改Makefile方式很多,只是此方式在写Makefile最为方便。
- 添加自己的Makefile
INC += -I./lua-5.4.2/src LUA_SRC=$(wildcard ./lua-5.4.2/src/*c) SRC=$(LUA_SRC) OBJS=${SRC:.c=.o} .c.o: g++ ${INC} -c $< ${CFLAGS} -o $@ #./lua-5.4.2/src/lapi.c\ #SRC = $(wildcard ./*.cc) TARGET=testLua TARGET:$(OBJS) g++ -o $(TARGET) $(INC) $(OBJS) testLua.cc
- make编译
3. testLua.cc //注:源码没整理。。。。
#include <stdio.h> #include <string.h> #include <exception> #include "a.h" using namespace std; //lin@lin-VirtualBox:~/pro/test/testLua/lua-5.4.2$ grep -nr "main(" // src/luac.c:169:static int pmain(lua_State* L) // src/luac.c:197:int main(int argc, char* argv[]) // ./lua-5.4.2/src/lua.c #include "lua.h" #include "lualib.h" #include "lauxlib.h" extern "C" { // #include "lua.h" // #include "lualib.h" // #include "lauxlib.h" } lua_State* L; void stackDump(lua_State* L) ; #if 1 int LuaAdd(int x,int y) { int sum; try { /* code */ //code5 printf("111.\n"); lua_getglobal(L, "add"); // lua_getglobal(L, "mytest"); //code6 printf("2.\n"); lua_pushnumber(L, x); printf("3.\n"); //code7 lua_pushnumber(L, y); printf("4.\n"); //code8 lua_call(L, 2, 1); printf("5.\n"); //code9 sum = (int)lua_tointeger(L, -1); printf("6.\n"); //code10 lua_pop(L, 1); printf("7.\n"); } catch(...)//catch(exception e) { stackDump(L); } return sum; } #endif void TestDemo() { const char* buff = "print(\"hello\")"; int error; lua_State* L = luaL_newstate(); luaL_openlibs(L); error = luaL_loadbuffer(L,buff,strlen(buff),"line") || lua_pcall(L,0,0,0); int s = lua_gettop(L); if (error) { fprintf(stderr,"%s",lua_tostring(L,-1)); lua_pop(L,1); } lua_close(L); } //该函数来自网络 #if 1 void stackDump(lua_State* L) { int i; int top = lua_gettop(L); printf("stackDump(num=%d):\n",top); for (i = 1; i <= top; i++) { /* repeat for each level */ int t = lua_type(L, i); switch (t) { case LUA_TSTRING: /* strings */ printf("`%s'", lua_tostring(L, i)); break; case LUA_TBOOLEAN: /* booleans */ printf(lua_toboolean(L, i) ? "true" : "false"); break; case LUA_TNUMBER: /* numbers */ printf("%g", lua_tonumber(L, i)); break; default: /* other values */ printf("%s", lua_typename(L, t)); break; } printf(" "); /* put a separator */ } printf("\n"); /* end the listing */ } #endif int main(int argc, char *argv[]) { // TestDemo(); // TestShow(); #if 1 // LUA int sum; //code1 // L = lua_open(); lua_State* L=luaL_newstate(); //code2 luaL_openlibs(L); //code3 int ret = luaL_dofile(L, "Test.lua"); printf("ret:%d\n",ret); if(ret) { printf("file no exist!!!!!!!!!!!!!!!1\n"); return 1; } //code4 printf("load file finish!!!\n"); sum = LuaAdd(2, 15); printf("The sum is: %d\n", sum); //code11 lua_close(L); printf("Press enter to exit..."); getchar(); #endif return 0; }
4. Test.Lua 脚本内容
print "Hello, Lua Demo2!" function add(x,y) return x + y end function mytest() print("mytest..") end print(mytest()) -- print("show value:",add(1,3))
5. 程序运行
- lua库编译使用的方式,已经验证是可行。
- 但程序仍然崩溃在lua_getglobal(L, "add"); 原因不明,后续跟进。
总结:
在单片机中调用lua脚本方式,通过这个思路让单片机灵活多变的需求有一个不错的方向。
脚本文件怎么保存在单片机?^-^答案自行搜索!
本文来自博客园,作者:ljymoonlight,转载请注明原文链接:https://www.cnblogs.com/ljymoonlight/p/14255238.html
posted on 2021-01-09 16:02 ljymoonlight 阅读(1267) 评论(1) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· winform 绘制太阳,地球,月球 运作规律
· 上周热点回顾(3.3-3.9)