個(gè)人網(wǎng)站可以做企業(yè)宣傳個(gè)人如何做seo推廣
Lua與C交互
- 1. 常見Lua相關(guān)的C API
- 壓入元素
- 查詢?cè)?/li>
- 獲取元素
- 檢查元素
- 棧的相關(guān)數(shù)據(jù)操作
- 2. C調(diào)用Lua
- 核心調(diào)用函數(shù)
- 示例
- 3. Lua調(diào)用C
- 1. C函數(shù)注冊(cè)到Lua(lua_register)
- 示例
- 2. 批量注冊(cè)(luaL_Reg)
- 示例
1. 常見Lua相關(guān)的C API
壓入元素
// cpp
void lua_pushnil(lua_State *L);
void lua_pushboolean(lua_State *L, int bool);
void lua_pushnumber(lua_State *L, lua_Number n);
void lua_pushinteger(lua_State *L, lua_Integer n);
void lua_pushlstring(lua_State *L, const char* s, size_t len);
void lua_pushstring(lua_State *L, const char* s);
void lua_pushfunction(lua_State *L, lua_CFunction fn);
查詢?cè)?/h3>
// cpp
int lua_is***(lua_State *L, int index); // 檢查lua數(shù)據(jù)類型 nil number string table等// lua_type 函數(shù)是 Lua C API 中用于獲取指定索引處的值的類型的函數(shù)。它返回一個(gè)表示值類型的整數(shù),并且不會(huì)改變堆棧上的內(nèi)容。
/* 如果索引處的值存在,則返回該值的類型,以整數(shù)形式表示。返回值為以下預(yù)定義的常量之一:
LUA_TNIL:空值。
LUA_TBOOLEAN:布爾值。
LUA_TLIGHTUSERDATA:輕量用戶數(shù)據(jù)。
LUA_TNUMBER:數(shù)字。
LUA_TSTRING:字符串。
LUA_TTABLE:表。
LUA_TFUNCTION:函數(shù)。
LUA_TUSERDATA:用戶數(shù)據(jù)。
LUA_TTHREAD:線程(協(xié)程)。
如果索引處的值不存在,則返回 LUA_TNONE。
*/
int lua_type(lua_State *L, int index);
獲取元素
// cpp
// to* 相關(guān)操作并不會(huì)改變lua棧,只是從棧中index出取值并轉(zhuǎn)成c變量類型int lua_toboolean(lua_State *L, int index); // 從棧中獲取bool值const char *lua_tostring(lua_State *L, int index);// len:用于存儲(chǔ)字符串長度的指針(可選參數(shù))。如果為 NULL,則不返回字符串長度
const char *lua_tolstring(lua_State *L, int index, size_t *len);// lua_Integer 通常在 Lua 的頭文件(如 lua.h 或 luaconf.h)中定義
// typedef long long lua_Integer
lua_Integer lua_tointeger(lua_State *L, int index);// lua_Number 通常在 Lua 的頭文件(如 lua.h 或 luaconf.h)中定義
// typedef double lua_Number;
lua_Number lua_tonumber(lua_State *L, int index);int lua_toboolean(lua_State *L, int index);
檢查元素
// cpp
int luaL_checkinteger(lua_State *L, int arg); lua_Number luaL_checknumber(lua_State *L, int arg);const char* luaL_checkstring(lua_State *L, int arg);int luaL_checkboolean(lua_State *L, int arg);// t:要檢查的類型,在 Lua 中表示為預(yù)定義的宏,例如 LUA_TNUMBER、LUA_TSTRING 等。
void luaL_checktype(lua_State *L, int arg, int t);
棧的相關(guān)數(shù)據(jù)操作
// cpp
lua_pop(lua_State *L, int n); // 彈出棧頂?shù)?n 個(gè)值。lua_gettop(lua_State *L); // 返回堆棧的棧頂索引(但不修改堆棧)。lua_settop(lua_State *L, int index);// 修改棧元素?cái)?shù)量,減小棧則會(huì)丟棄多余部分元素,增大會(huì)push nil值lua_remove(lua_State *L, int index); // 移除指定索引處的值,并將上面的所有值下移。// 用于將指定索引處的值復(fù)制到堆棧頂部。它不會(huì)刪除原始值,而是將其復(fù)制一份并推入堆棧。
void lua_pushvalue(lua_State *L, int index);// 用于將棧頂?shù)闹祻棾?#xff0c;并將其設(shè)置為lua的全局變量
void lua_setglobal(lua_State *L, const char *name);// 用于將lua全局變量的值推入堆棧
void lua_getglobal(lua_State *L, const char *name);// 用于將棧頂?shù)闹祻棾?#xff0c;并將其設(shè)置為index處的表中指定字段的值
/* lua_setfield 從棧頂彈出一個(gè)值,并將其設(shè)置為表中指定字段的值。
這個(gè)操作相當(dāng)于在 Lua 中執(zhí)行 t[k] = value,其中 t 是棧中索引為 index 的表,k 是字段名,value 是棧頂?shù)闹怠?/
void lua_setfield(lua_State *L, int index, const char *k);// 用于從指定索引處的表中獲取一個(gè)字段的值,并將其推入堆棧
void lua_getfield(lua_State *L, int index, const char *k);
2. C調(diào)用Lua
核心調(diào)用函數(shù)
void lua_call(lua_State *L, int nargs, int nresults); // 相對(duì)lua_pcall來說至少了錯(cuò)誤處理函數(shù)int lua_pcall(lua_State *L, int nargs, int nresults, int errfunc);
/* lua_State *L:指向 Lua 狀態(tài)的指針。
int nargs:要傳遞給 Lua 函數(shù)的參數(shù)數(shù)量。
int nresults:Lua 函數(shù)預(yù)期返回的結(jié)果數(shù)量。
int errfunc:錯(cuò)誤處理函數(shù)在堆棧中的索引。通常為 0,表示沒有錯(cuò)誤處理函數(shù)。0:調(diào)用成功。
非零值:調(diào)用失敗,對(duì)應(yīng)于不同的錯(cuò)誤代碼,例如 LUA_ERRRUN、LUA_ERRMEM、LUA_ERRERR 等。lua_call和lua_pcall 執(zhí)行時(shí)在堆棧上進(jìn)行以下操作:
從棧頂開始,依次向下計(jì)算 nargs 個(gè)元素,這些元素表示傳遞給函數(shù)的參數(shù),會(huì)依次彈出這幾個(gè)參數(shù)。
在參數(shù)之后的那個(gè)元素應(yīng)該是要調(diào)用的函數(shù)。
彈出函數(shù)本身,調(diào)用函數(shù),并期望 nresults 個(gè)返回值,壓入 nresults 個(gè)返回值。
*/
示例
-- Lua
function add(a, b)return a + b
end
// cpp
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <stdio.h>int main() {lua_State *L = luaL_newstate(); // 創(chuàng)建新的 Lua 狀態(tài)luaL_openlibs(L); // 打開標(biāo)準(zhǔn)庫if (luaL_dofile(L, "script.lua")) { // 加載并執(zhí)行 Lua 腳本fprintf(stderr, "Failed to load script: %s\n", lua_tostring(L, -1));return 1;}lua_getglobal(L, "add"); // 將全局函數(shù) "add" 壓入堆棧if (!lua_isfunction(L, -1)) {fprintf(stderr, "'add' is not a function\n");return 1;}lua_pushnumber(L, 10); // 壓入第一個(gè)參數(shù)lua_pushnumber(L, 20); // 壓入第二個(gè)參數(shù)// lua_call(L, 2, 1); // 調(diào)用函數(shù),傳遞 2 個(gè)參數(shù),期望 1 個(gè)結(jié)果if (lua_pcall(L, 2, 1, 0) != 0) { // 調(diào)用函數(shù),傳遞 2 個(gè)參數(shù),期望 1 個(gè)結(jié)果fprintf(stderr, "Error calling 'add': %s\n", lua_tostring(L, -1));return 1;}if (lua_isnumber(L, -1)) {double result = lua_tonumber(L, -1);printf("Result: %f\n", result); // 打印結(jié)果} else {fprintf(stderr, "Function 'add' did not return a number\n");}lua_pop(L, 1); // 從堆棧中移除結(jié)果lua_close(L); // 關(guān)閉 Lua 狀態(tài)return 0;
}
3. Lua調(diào)用C
Lua調(diào)用C函數(shù)時(shí),必須遵守int FunctionName(lua_State *L)類型去定義并實(shí)現(xiàn),其中int返回值表示參數(shù)個(gè)數(shù)。當(dāng)每個(gè)Lua調(diào)用C函數(shù)時(shí),會(huì)自動(dòng)在內(nèi)部維護(hù)一個(gè)私有局部棧,因此我們?nèi)?shù)時(shí)直接從1取就可以,且在調(diào)用時(shí)與結(jié)束調(diào)用時(shí)無需考慮棧的清理問題。
1. C函數(shù)注冊(cè)到Lua(lua_register)
// lua_register 是 Lua C API 中的一個(gè)宏,用于將 C 函數(shù)注冊(cè)為 Lua 全局函數(shù)/* 這個(gè)宏實(shí)際上是 lua_pushcfunction 和 lua_setglobal 兩個(gè)函數(shù)的組合:
lua_pushcfunction(L, f):將 C 函數(shù) f 壓入 Lua 堆棧。
lua_setglobal(L, n):將堆棧頂部的值(即剛剛壓入的 C 函數(shù) f)設(shè)置為全局變量 n。*/#define lua_register(L, n, f) \(lua_pushcfunction(L, (f)), lua_setglobal(L, (n)))
示例
-- script.lua
local result = add(10, 20)
print("Result of add(10, 20):", result)
// cpp
#include <iostream>
#include <lua.hpp>// C++ 函數(shù)
int add(lua_State* L) {int a = luaL_checkinteger(L, 1);int b = luaL_checkinteger(L, 2);lua_pushinteger(L, a + b);return 1;
}// 將 C++ 函數(shù)注冊(cè)到 Lua
void register_functions(lua_State* L) {lua_register(L, "add", add);
}int main() {lua_State* L = luaL_newstate(); // 創(chuàng)建新的 Lua 狀態(tài)luaL_openlibs(L); // 打開標(biāo)準(zhǔn)庫register_functions(L); // 注冊(cè) C++ 函數(shù)if (luaL_dofile(L, "script.lua")) { // 執(zhí)行 Lua 腳本std::cerr << "Failed to load script: " << lua_tostring(L, -1) << std::endl;lua_pop(L, 1); // 從堆棧中移除錯(cuò)誤消息}lua_close(L); // 關(guān)閉 Lua 狀態(tài)return 0;
}
2. 批量注冊(cè)(luaL_Reg)
// cpp
typedef struct luaL_Reg {const char *name; // 函數(shù)的名字lua_CFunction func; // 對(duì)應(yīng)的 C 函數(shù)
} luaL_Reg;void luaL_newlib (lua_State *L, const luaL_Reg l[]);
/*這個(gè)實(shí)際上是 luaL_newlibtable 和 luaL_setfuncs兩個(gè)函數(shù)的組合:void luaL_newlibtable(lua_State *L, const luaL_Reg l[]);
其中l(wèi)為指向 luaL_Reg 結(jié)構(gòu)體數(shù)組的指針,該數(shù)組定義了庫中的函數(shù)。
luaL_newlibtable 是 Lua C API 提供的一個(gè)函數(shù),用于創(chuàng)建一個(gè)新的空表,
這個(gè)表的大小預(yù)分配為注冊(cè)表中定義的庫函數(shù)數(shù)量。
這在創(chuàng)建一個(gè)新的 Lua 庫時(shí)特別有用,因?yàn)樗梢灶A(yù)先分配合適的空間以容納所有的庫函數(shù),避免動(dòng)態(tài)擴(kuò)展表的開銷。void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup);
l:指向 luaL_Reg 結(jié)構(gòu)體數(shù)組的指針,該數(shù)組定義了庫中的函數(shù)。
nup:傳遞給每個(gè)函數(shù)的 upvalue 數(shù)量。通常為 0。
luaL_setfuncs用于將 C 函數(shù)數(shù)組注冊(cè)到一個(gè) Lua 表中。
*/
示例
// cpp
#include <lua.hpp>
#include <iostream>// C 函數(shù):加法
int add(lua_State* L) {int a = luaL_checkinteger(L, 1);int b = luaL_checkinteger(L, 2);lua_pushinteger(L, a + b);return 1;
}// C 函數(shù):減法
int sub(lua_State* L) {int a = luaL_checkinteger(L, 1);int b = luaL_checkinteger(L, 2);lua_pushinteger(L, a - b);return 1;
}// 創(chuàng)建 Lua 庫
extern "C" int luaopen_mylib(lua_State* L) {luaL_Reg mylib[] = {{"add", add},{"sub", sub},{NULL, NULL}};luaL_newlib(L, mylib);return 1;
}int main() {lua_State* L = luaL_newstate();luaL_openlibs(L);luaopen_mylib(L); // 手動(dòng)打開庫(如果需要)if (luaL_dofile(L, "script.lua")) {std::cerr << "Failed to load script: " << lua_tostring(L, -1) << std::endl;lua_pop(L, 1);}lua_close(L);return 0;
}
-- script.lua
local mylib = require("mylib")local result_add = mylib.add(10, 20)
print("Result of add(10, 20):", result_add)local result_sub = mylib.sub(20, 10)
print("Result of sub(20, 10):", result_sub)
// cpp
int lua_is***(lua_State *L, int index); // 檢查lua數(shù)據(jù)類型 nil number string table等// lua_type 函數(shù)是 Lua C API 中用于獲取指定索引處的值的類型的函數(shù)。它返回一個(gè)表示值類型的整數(shù),并且不會(huì)改變堆棧上的內(nèi)容。
/* 如果索引處的值存在,則返回該值的類型,以整數(shù)形式表示。返回值為以下預(yù)定義的常量之一:
LUA_TNIL:空值。
LUA_TBOOLEAN:布爾值。
LUA_TLIGHTUSERDATA:輕量用戶數(shù)據(jù)。
LUA_TNUMBER:數(shù)字。
LUA_TSTRING:字符串。
LUA_TTABLE:表。
LUA_TFUNCTION:函數(shù)。
LUA_TUSERDATA:用戶數(shù)據(jù)。
LUA_TTHREAD:線程(協(xié)程)。
如果索引處的值不存在,則返回 LUA_TNONE。
*/
int lua_type(lua_State *L, int index);
// cpp
// to* 相關(guān)操作并不會(huì)改變lua棧,只是從棧中index出取值并轉(zhuǎn)成c變量類型int lua_toboolean(lua_State *L, int index); // 從棧中獲取bool值const char *lua_tostring(lua_State *L, int index);// len:用于存儲(chǔ)字符串長度的指針(可選參數(shù))。如果為 NULL,則不返回字符串長度
const char *lua_tolstring(lua_State *L, int index, size_t *len);// lua_Integer 通常在 Lua 的頭文件(如 lua.h 或 luaconf.h)中定義
// typedef long long lua_Integer
lua_Integer lua_tointeger(lua_State *L, int index);// lua_Number 通常在 Lua 的頭文件(如 lua.h 或 luaconf.h)中定義
// typedef double lua_Number;
lua_Number lua_tonumber(lua_State *L, int index);int lua_toboolean(lua_State *L, int index);
// cpp
int luaL_checkinteger(lua_State *L, int arg); lua_Number luaL_checknumber(lua_State *L, int arg);const char* luaL_checkstring(lua_State *L, int arg);int luaL_checkboolean(lua_State *L, int arg);// t:要檢查的類型,在 Lua 中表示為預(yù)定義的宏,例如 LUA_TNUMBER、LUA_TSTRING 等。
void luaL_checktype(lua_State *L, int arg, int t);
// cpp
lua_pop(lua_State *L, int n); // 彈出棧頂?shù)?n 個(gè)值。lua_gettop(lua_State *L); // 返回堆棧的棧頂索引(但不修改堆棧)。lua_settop(lua_State *L, int index);// 修改棧元素?cái)?shù)量,減小棧則會(huì)丟棄多余部分元素,增大會(huì)push nil值lua_remove(lua_State *L, int index); // 移除指定索引處的值,并將上面的所有值下移。// 用于將指定索引處的值復(fù)制到堆棧頂部。它不會(huì)刪除原始值,而是將其復(fù)制一份并推入堆棧。
void lua_pushvalue(lua_State *L, int index);// 用于將棧頂?shù)闹祻棾?#xff0c;并將其設(shè)置為lua的全局變量
void lua_setglobal(lua_State *L, const char *name);// 用于將lua全局變量的值推入堆棧
void lua_getglobal(lua_State *L, const char *name);// 用于將棧頂?shù)闹祻棾?#xff0c;并將其設(shè)置為index處的表中指定字段的值
/* lua_setfield 從棧頂彈出一個(gè)值,并將其設(shè)置為表中指定字段的值。
這個(gè)操作相當(dāng)于在 Lua 中執(zhí)行 t[k] = value,其中 t 是棧中索引為 index 的表,k 是字段名,value 是棧頂?shù)闹怠?/
void lua_setfield(lua_State *L, int index, const char *k);// 用于從指定索引處的表中獲取一個(gè)字段的值,并將其推入堆棧
void lua_getfield(lua_State *L, int index, const char *k);
void lua_call(lua_State *L, int nargs, int nresults); // 相對(duì)lua_pcall來說至少了錯(cuò)誤處理函數(shù)int lua_pcall(lua_State *L, int nargs, int nresults, int errfunc);
/* lua_State *L:指向 Lua 狀態(tài)的指針。
int nargs:要傳遞給 Lua 函數(shù)的參數(shù)數(shù)量。
int nresults:Lua 函數(shù)預(yù)期返回的結(jié)果數(shù)量。
int errfunc:錯(cuò)誤處理函數(shù)在堆棧中的索引。通常為 0,表示沒有錯(cuò)誤處理函數(shù)。0:調(diào)用成功。
非零值:調(diào)用失敗,對(duì)應(yīng)于不同的錯(cuò)誤代碼,例如 LUA_ERRRUN、LUA_ERRMEM、LUA_ERRERR 等。lua_call和lua_pcall 執(zhí)行時(shí)在堆棧上進(jìn)行以下操作:
從棧頂開始,依次向下計(jì)算 nargs 個(gè)元素,這些元素表示傳遞給函數(shù)的參數(shù),會(huì)依次彈出這幾個(gè)參數(shù)。
在參數(shù)之后的那個(gè)元素應(yīng)該是要調(diào)用的函數(shù)。
彈出函數(shù)本身,調(diào)用函數(shù),并期望 nresults 個(gè)返回值,壓入 nresults 個(gè)返回值。
*/
-- Lua
function add(a, b)return a + b
end
// cpp
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <stdio.h>int main() {lua_State *L = luaL_newstate(); // 創(chuàng)建新的 Lua 狀態(tài)luaL_openlibs(L); // 打開標(biāo)準(zhǔn)庫if (luaL_dofile(L, "script.lua")) { // 加載并執(zhí)行 Lua 腳本fprintf(stderr, "Failed to load script: %s\n", lua_tostring(L, -1));return 1;}lua_getglobal(L, "add"); // 將全局函數(shù) "add" 壓入堆棧if (!lua_isfunction(L, -1)) {fprintf(stderr, "'add' is not a function\n");return 1;}lua_pushnumber(L, 10); // 壓入第一個(gè)參數(shù)lua_pushnumber(L, 20); // 壓入第二個(gè)參數(shù)// lua_call(L, 2, 1); // 調(diào)用函數(shù),傳遞 2 個(gè)參數(shù),期望 1 個(gè)結(jié)果if (lua_pcall(L, 2, 1, 0) != 0) { // 調(diào)用函數(shù),傳遞 2 個(gè)參數(shù),期望 1 個(gè)結(jié)果fprintf(stderr, "Error calling 'add': %s\n", lua_tostring(L, -1));return 1;}if (lua_isnumber(L, -1)) {double result = lua_tonumber(L, -1);printf("Result: %f\n", result); // 打印結(jié)果} else {fprintf(stderr, "Function 'add' did not return a number\n");}lua_pop(L, 1); // 從堆棧中移除結(jié)果lua_close(L); // 關(guān)閉 Lua 狀態(tài)return 0;
}
Lua調(diào)用C函數(shù)時(shí),必須遵守int FunctionName(lua_State *L)類型去定義并實(shí)現(xiàn),其中int返回值表示參數(shù)個(gè)數(shù)。當(dāng)每個(gè)Lua調(diào)用C函數(shù)時(shí),會(huì)自動(dòng)在內(nèi)部維護(hù)一個(gè)私有局部棧,因此我們?nèi)?shù)時(shí)直接從1取就可以,且在調(diào)用時(shí)與結(jié)束調(diào)用時(shí)無需考慮棧的清理問題。
// lua_register 是 Lua C API 中的一個(gè)宏,用于將 C 函數(shù)注冊(cè)為 Lua 全局函數(shù)/* 這個(gè)宏實(shí)際上是 lua_pushcfunction 和 lua_setglobal 兩個(gè)函數(shù)的組合:
lua_pushcfunction(L, f):將 C 函數(shù) f 壓入 Lua 堆棧。
lua_setglobal(L, n):將堆棧頂部的值(即剛剛壓入的 C 函數(shù) f)設(shè)置為全局變量 n。*/#define lua_register(L, n, f) \(lua_pushcfunction(L, (f)), lua_setglobal(L, (n)))
-- script.lua
local result = add(10, 20)
print("Result of add(10, 20):", result)
// cpp
#include <iostream>
#include <lua.hpp>// C++ 函數(shù)
int add(lua_State* L) {int a = luaL_checkinteger(L, 1);int b = luaL_checkinteger(L, 2);lua_pushinteger(L, a + b);return 1;
}// 將 C++ 函數(shù)注冊(cè)到 Lua
void register_functions(lua_State* L) {lua_register(L, "add", add);
}int main() {lua_State* L = luaL_newstate(); // 創(chuàng)建新的 Lua 狀態(tài)luaL_openlibs(L); // 打開標(biāo)準(zhǔn)庫register_functions(L); // 注冊(cè) C++ 函數(shù)if (luaL_dofile(L, "script.lua")) { // 執(zhí)行 Lua 腳本std::cerr << "Failed to load script: " << lua_tostring(L, -1) << std::endl;lua_pop(L, 1); // 從堆棧中移除錯(cuò)誤消息}lua_close(L); // 關(guān)閉 Lua 狀態(tài)return 0;
}
// cpp
typedef struct luaL_Reg {const char *name; // 函數(shù)的名字lua_CFunction func; // 對(duì)應(yīng)的 C 函數(shù)
} luaL_Reg;void luaL_newlib (lua_State *L, const luaL_Reg l[]);
/*這個(gè)實(shí)際上是 luaL_newlibtable 和 luaL_setfuncs兩個(gè)函數(shù)的組合:void luaL_newlibtable(lua_State *L, const luaL_Reg l[]);
其中l(wèi)為指向 luaL_Reg 結(jié)構(gòu)體數(shù)組的指針,該數(shù)組定義了庫中的函數(shù)。
luaL_newlibtable 是 Lua C API 提供的一個(gè)函數(shù),用于創(chuàng)建一個(gè)新的空表,
這個(gè)表的大小預(yù)分配為注冊(cè)表中定義的庫函數(shù)數(shù)量。
這在創(chuàng)建一個(gè)新的 Lua 庫時(shí)特別有用,因?yàn)樗梢灶A(yù)先分配合適的空間以容納所有的庫函數(shù),避免動(dòng)態(tài)擴(kuò)展表的開銷。void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup);
l:指向 luaL_Reg 結(jié)構(gòu)體數(shù)組的指針,該數(shù)組定義了庫中的函數(shù)。
nup:傳遞給每個(gè)函數(shù)的 upvalue 數(shù)量。通常為 0。
luaL_setfuncs用于將 C 函數(shù)數(shù)組注冊(cè)到一個(gè) Lua 表中。
*/
// cpp
#include <lua.hpp>
#include <iostream>// C 函數(shù):加法
int add(lua_State* L) {int a = luaL_checkinteger(L, 1);int b = luaL_checkinteger(L, 2);lua_pushinteger(L, a + b);return 1;
}// C 函數(shù):減法
int sub(lua_State* L) {int a = luaL_checkinteger(L, 1);int b = luaL_checkinteger(L, 2);lua_pushinteger(L, a - b);return 1;
}// 創(chuàng)建 Lua 庫
extern "C" int luaopen_mylib(lua_State* L) {luaL_Reg mylib[] = {{"add", add},{"sub", sub},{NULL, NULL}};luaL_newlib(L, mylib);return 1;
}int main() {lua_State* L = luaL_newstate();luaL_openlibs(L);luaopen_mylib(L); // 手動(dòng)打開庫(如果需要)if (luaL_dofile(L, "script.lua")) {std::cerr << "Failed to load script: " << lua_tostring(L, -1) << std::endl;lua_pop(L, 1);}lua_close(L);return 0;
}
-- script.lua
local mylib = require("mylib")local result_add = mylib.add(10, 20)
print("Result of add(10, 20):", result_add)local result_sub = mylib.sub(20, 10)
print("Result of sub(20, 10):", result_sub)