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

文件内容

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

lua 复制代码
print("hello world")

-- 引入其他模块
--dofile("module.lua")


-- require函数用于引入其他模块, 以下2种引入方式都可以
--require("module")
require "module"

print("hello world")

-- 单行注释

--[[
多行注释
]]--

-- 全局变量
a = 1;

-- 局部变量
local aa = 1;

-- 数据类型
local num = 1 -- number
local num = 1.0009999 -- number
local str = "hello" -- string
local bool =  false -- boolean
local bool =  {"1","2"} -- table
local nil_var = nil -- nil 表示无效的值,和java的null 有区别,相当于java的false
local invalid_var -- 当一个变量未赋值,它的默认值就是 nil


-- type 函数,输出对象的类型
print(type("i love you"))  -- string
print(type(false)) -- boolean
print(type(nil)) -- nil
print(type(1)) -- number
print(type(1.111)) -- number
print(type(type)) -- function


-- if语法
if true then
    print("条件为真")
end

-- if else
if false then
    print("条件为真")
else
    print("条件为假")
end


-- if else if
local ifA = 21;
if ifA == 1 then
    print("为1")
elseif ifA == 2 then
    print("为2")
elseif ifA == 3 then
    print("为4")
else
    print("啥也不是")
end


-- while 循环
local num = 0;
while (num < 20) -- while判断条件为ture时会一直执行do循环体的语句
do
    print("current num"..num)
    num =num+1
end

-- for循环
-- i=1 : 声明变量
-- 10: i只要不超过10,就会一直执行do循环体的语句,
-- 2: i每次递增的数量
for i = 1, 10 ,2
do
    print("i的值为"..i)
end


-- repeat ..until() 满足条件结束
local c = 1
repeat
    print("c的值:"..c)
    c = c+1
until(c<10) -- 当条件为true时结束循环,否则一直执行循环体的内容

-- 函数
function add(a,b)
    return a + b
end
print(add(1,2))


-- 两个点 .. 表示拼接
print("hello".." world!!!!")
local age = 6
print("我已经"..age.."岁了")

-- 不能拼接nil值,会报错
local nil_con = nil
--print("nil_con的值为:"..nil_con)


-- 表 可以当数组用,也可以当 map 或对象来用

local table = {} -- 初始化表
table[1] = 1 --给表赋值
table["key"] = "val"
-- 表取值
print("1的位置值为:"..table[1])
print("key的位置值为:"..table["key"])
--print("key1的位置值为:"..table["key1"]) -- 获取一个不存在的值会报错
-- 移除引用
table = nil

--print("1的位置值为:"..table[1])-- 因为上面已经移除了表的引用,在这里取值会报错



-- 调用模块的函数
module.fun("yexindong")

-- 打印模块的常量
print(module.constant)

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

lua 复制代码
-- 模块

-- 模块相当于一个封装库, 也可以理解为是java的一个class
-- 从 Lua 5.1 开始,Lua 加入了标准的模块管理机制,可以把一些公用的代码放在一个文件里,以 API 接口的形式在其他地方调用,有利于代码的重用和降低代码耦合度


-- 定义一个名为module的模块
module = {}

-- 定义一个常量
module.constant = "this is constant"

-- 定义私有函数,只能在内部调用
local function private_func(a)
    print("调用了私有函数"..a)
end

-- 定义一个函数
function module.fun(a)
    print("a的值为:"..a)
    private_func(a)
end

-- 一定要return出去,否则其他地方无法调用
return module

main.c , c语言入口

c 复制代码
#include <stdio.h>
#include  <lua.h>
#include  "lualib.h"
#include  "lauxlib.h"
int main() {
    printf("Hello, World!\n");
    lua_State* L;
    L = luaL_newstate(); /* 创建lua状态机 */
    printf("L:%p\n",L);

    luaL_openlibs(L); // 打开Lua标准库

    // 调用lua脚本,Main.lua 又调用了另一个lua脚本
    // 通过 dofile 或者 require引入都可以调用另一个脚本,前提是这2个脚本必须放在一起,且必须和 mian 函数打包后的可执行文件放在一起,否则会报错,找不到 module
    if (luaL_dofile(L, "Main.lua") != LUA_OK) {
        printf("Error: %s\n", lua_tostring(L, -1));
        return 1;
    }

    lua_close(L); // 关闭Lua状态机
    return 0;
}

CMakeLists.txt

shell 复制代码
cmake_minimum_required(VERSION 3.23)
project(untitled C)

set(CMAKE_C_STANDARD 11)


add_executable(untitled main.c)
target_link_libraries(untitled  -llua -ldl -lm)

# 复制文件到项目的根目录
configure_file( Main.lua Main.lua COPYONLY)
configure_file( module.lua module.lua COPYONLY)

# 注意:一定要加到最后面
target_include_directories(untitled PUBLIC /usr/local/include/)

文件目录

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

复制代码
project
  └--cmake-build-debug
  └--CMakeLists.txt
  └--main.c
  └--Main.lua
  └--module.lua

开始执行

生成 Makefile

复制代码
cmake .

编译

复制代码
make

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

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