C/C++ 调用lua脚本,lua脚本调用另一个lua脚本

发布时间:2024-04-28 16:08:44 作者:yexindonglai@163.com 阅读(86)

文件内容

Main.lua , 被c/c++调用的lua

  1. print("hello world")
  2. -- 引入其他模块
  3. --dofile("module.lua")
  4. -- require函数用于引入其他模块, 以下2种引入方式都可以
  5. --require("module")
  6. require "module"
  7. print("hello world")
  8. -- 单行注释
  9. --[[
  10. 多行注释
  11. ]]--
  12. -- 全局变量
  13. a = 1;
  14. -- 局部变量
  15. local aa = 1;
  16. -- 数据类型
  17. local num = 1 -- number
  18. local num = 1.0009999 -- number
  19. local str = "hello" -- string
  20. local bool = false -- boolean
  21. local bool = {"1","2"} -- table
  22. local nil_var = nil -- nil 表示无效的值,和javanull 有区别,相当于javafalse
  23. local invalid_var -- 当一个变量未赋值,它的默认值就是 nil
  24. -- type 函数,输出对象的类型
  25. print(type("i love you")) -- string
  26. print(type(false)) -- boolean
  27. print(type(nil)) -- nil
  28. print(type(1)) -- number
  29. print(type(1.111)) -- number
  30. print(type(type)) -- function
  31. -- if语法
  32. if true then
  33. print("条件为真")
  34. end
  35. -- if else
  36. if false then
  37. print("条件为真")
  38. else
  39. print("条件为假")
  40. end
  41. -- if else if
  42. local ifA = 21;
  43. if ifA == 1 then
  44. print("为1")
  45. elseif ifA == 2 then
  46. print("为2")
  47. elseif ifA == 3 then
  48. print("为4")
  49. else
  50. print("啥也不是")
  51. end
  52. -- while 循环
  53. local num = 0;
  54. while (num < 20) -- while判断条件为ture时会一直执行do循环体的语句
  55. do
  56. print("current num"..num)
  57. num =num+1
  58. end
  59. -- for循环
  60. -- i=1 声明变量
  61. -- 10 i只要不超过10,就会一直执行do循环体的语句,
  62. -- 2 i每次递增的数量
  63. for i = 1, 10 ,2
  64. do
  65. print("i的值为"..i)
  66. end
  67. -- repeat ..until() 满足条件结束
  68. local c = 1
  69. repeat
  70. print("c的值:"..c)
  71. c = c+1
  72. until(c<10) -- 当条件为true时结束循环,否则一直执行循环体的内容
  73. -- 函数
  74. function add(a,b)
  75. return a + b
  76. end
  77. print(add(1,2))
  78. -- 两个点 .. 表示拼接
  79. print("hello".." world!!!!")
  80. local age = 6
  81. print("我已经"..age.."岁了")
  82. -- 不能拼接nil值,会报错
  83. local nil_con = nil
  84. --print("nil_con的值为:"..nil_con)
  85. -- 可以当数组用,也可以当 map 或对象来用
  86. local table = {} -- 初始化表
  87. table[1] = 1 --给表赋值
  88. table["key"] = "val"
  89. -- 表取值
  90. print("1的位置值为:"..table[1])
  91. print("key的位置值为:"..table["key"])
  92. --print("key1的位置值为:"..table["key1"]) -- 获取一个不存在的值会报错
  93. -- 移除引用
  94. table = nil
  95. --print("1的位置值为:"..table[1])-- 因为上面已经移除了表的引用,在这里取值会报错
  96. -- 调用模块的函数
  97. module.fun("yexindong")
  98. -- 打印模块的常量
  99. print(module.constant)

module.lua , 被lua调用的lua模块

  1. -- 模块
  2. -- 模块相当于一个封装库, 也可以理解为是java的一个class
  3. -- Lua 5.1 开始,Lua 加入了标准的模块管理机制,可以把一些公用的代码放在一个文件里,以 API 接口的形式在其他地方调用,有利于代码的重用和降低代码耦合度
  4. -- 定义一个名为module的模块
  5. module = {}
  6. -- 定义一个常量
  7. module.constant = "this is constant"
  8. -- 定义私有函数,只能在内部调用
  9. local function private_func(a)
  10. print("调用了私有函数"..a)
  11. end
  12. -- 定义一个函数
  13. function module.fun(a)
  14. print("a的值为:"..a)
  15. private_func(a)
  16. end
  17. -- 一定要return出去,否则其他地方无法调用
  18. return module

main.c , c语言入口

  1. #include <stdio.h>
  2. #include <lua.h>
  3. #include "lualib.h"
  4. #include "lauxlib.h"
  5. int main() {
  6. printf("Hello, World!\n");
  7. lua_State* L;
  8. L = luaL_newstate(); /* 创建lua状态机 */
  9. printf("L:%p\n",L);
  10. luaL_openlibs(L); // 打开Lua标准库
  11. // 调用lua脚本,Main.lua 又调用了另一个lua脚本
  12. // 通过 dofile 或者 require引入都可以调用另一个脚本,前提是这2个脚本必须放在一起,且必须和 mian 函数打包后的可执行文件放在一起,否则会报错,找不到 module
  13. if (luaL_dofile(L, "Main.lua") != LUA_OK) {
  14. printf("Error: %s\n", lua_tostring(L, -1));
  15. return 1;
  16. }
  17. lua_close(L); // 关闭Lua状态机
  18. return 0;
  19. }

CMakeLists.txt

  1. cmake_minimum_required(VERSION 3.23)
  2. project(untitled C)
  3. set(CMAKE_C_STANDARD 11)
  4. add_executable(untitled main.c)
  5. target_link_libraries(untitled -llua -ldl -lm)
  6. # 复制文件到项目的根目录
  7. configure_file( Main.lua Main.lua COPYONLY)
  8. configure_file( module.lua module.lua COPYONLY)
  9. # 注意:一定要加到最后面
  10. target_include_directories(untitled PUBLIC /usr/local/include/)

文件目录

文件目录如下,一定要将所有文件都放在同一个目录下

  1. project
  2. └--cmake-build-debug
  3. └--CMakeLists.txt
  4. └--main.c
  5. └--Main.lua
  6. └--module.lua

开始执行

生成 Makefile

  1. cmake .

编译

  1. make

编译后可执行文件和lua脚本是放在一起的,这也是我想要的,必须要放在一起,否则执行会报错;

  1. ot@PAw9033927:/tmp/tmp.TYQSMrDA9B/cmake-build-debug# ls -l
  2. total 732
  3. -rw-r--r-- 1 root root 23927 Apr 25 16:46 CMakeCache.txt
  4. drwxr-xr-x 1 root root 4096 Apr 28 15:46 CMakeFiles
  5. -rw-r--r-- 1 root root 2794 Apr 28 15:44 Main.lua // lua脚本文件
  6. -rw-r--r-- 1 root root 5260 Apr 28 15:44 Makefile
  7. drwxr-xr-x 1 root root 4096 Apr 25 16:47 Testing
  8. -rw-r--r-- 1 root root 1631 Apr 25 16:46 cmake_install.cmake
  9. -rw-r--r-- 1 root root 865 Apr 25 16:46 module.lua // lua脚本文件
  10. -rwxr-xr-x 1 root root 303688 Apr 28 15:44 untitled // 可执行文件
  11. -rw-r--r-- 1 root root 4948 Apr 28 15:44 untitled.cbp

关键字Lua