網(wǎng)站建設(shè)教程模板網(wǎng)站排名seo軟件
系列文章目錄
lua調(diào)用C/C++的函數(shù),十分鐘快速掌握
C++調(diào)用lua腳本,包括全局函數(shù)綁定、類綁定,十分鐘快速掌握
- 系列文章目錄
- 摘要
- 環(huán)境
- 使用步驟
- 碼代碼
- 自定義函數(shù)
- 多返回值
- 變長(zhǎng)參數(shù)
- 自定義類
- test_sol2.lua內(nèi)容
- 程序輸出
摘要
在這個(gè)快節(jié)奏的技術(shù)博客中,我們將借助sol2庫(kù),以十分鐘的時(shí)間快速掌握如何在C++中調(diào)用Lua腳本。sol2是一個(gè)輕量級(jí)的Lua綁定庫(kù),它簡(jiǎn)化了C++與Lua之間的交互,使得全局函數(shù)和類的綁定變得異常簡(jiǎn)便。我們將創(chuàng)建幾個(gè)”復(fù)雜的“函數(shù),變長(zhǎng)參數(shù)、多返回值,和一個(gè)具有幾個(gè)成員函數(shù)的簡(jiǎn)單類,通過(guò)sol2的簡(jiǎn)潔語(yǔ)法,只需幾行代碼,我們就能將C++函數(shù)綁定到Lua環(huán)境中,實(shí)現(xiàn)跨語(yǔ)言調(diào)用。
為了快速輸出我們要講的內(nèi)容,我不僅提供了實(shí)際的示例代碼,還將解釋每一步的操作,確保能夠迅速理解并運(yùn)用這一技術(shù)。通過(guò)這十分鐘的快速掌握,希望將能夠在項(xiàng)目中充分發(fā)揮C++與Lua的優(yōu)勢(shì),實(shí)現(xiàn)更靈活、高效的代碼編寫(xiě)。無(wú)論是全局函數(shù)的調(diào)用還是類的綁定,sol2為C++與Lua的融合提供了一種極具便利性和效率的解決方案。
環(huán)境
工具\(yùn)組件 | 版本 | 說(shuō)明 |
---|---|---|
Windows | 10 | |
visual studio | 2022 | |
lua | 5.4 | |
sol2 | 3.2.3 | |
C++ | 17 | sol2需要 |
使用步驟
lua頭文件和靜態(tài)鏈接庫(kù)或者動(dòng)態(tài)庫(kù),從官方網(wǎng)站下載預(yù)編譯好的即可,當(dāng)然如果你有自定義需求也可自行修改編譯。
引入sol2頭文件,從github上下載。
# 就是這三個(gè)文件,要按此層次結(jié)構(gòu)放好。
│ sol.hpp
│
└─solconfig.hppforward.hpp
visual studio新建個(gè)項(xiàng)目,C++語(yǔ)言標(biāo)準(zhǔn)設(shè)置為C++17。
加入頭文件和庫(kù)文件到項(xiàng)目中。
接下來(lái)就是碼代碼了。
碼代碼
自定義函數(shù)
多返回值
通過(guò)使用sol2我們不需要自己再寫(xiě)包裝方法,這一下就省了不少代碼。
std::tuple<int,const char*, double> multipleResultFunc(int a, const char* b, double c)
{//return std::tuple<int, char*, double>(a, b, c);return std::make_tuple(a, b, c);
}
變長(zhǎng)參數(shù)
int variaArgs(sol::variadic_args va)
{int r = 0;for (auto v : va){r += static_cast<int>( v);}return r;
}
自定義類
class my_class{private:std::string m_str;public:int b = 24;int f() const{return 24;}void g(){++b;}void setTitle(const char* str){this->m_str = str;}void print(){std::cout << this->m_str << " , " << this->b << std::endl;std::wcout << this->rowname << L" , " << typeid( this->columns).name() << std::endl;}}
#define SOL_CHECK_ARGUMENTS 1
#include "sol.hpp"sol::state lua;lua.open_libraries(sol::lib::base);// "bark" namespacing in Lua// namespacing is just putting things in a tablesol::table bark = lua.create_named_table("bark");bark.new_usertype<my_class>("my_class","f", &my_class::f,"g", &my_class::g,"setTitle", &my_class::setTitle,"print", &my_class::print//,//"putUnsetValue", &my_class::putUnsetValue//,//"setSome", &my_class::setSome); // the usuallua.set_function("multipleResultFunc", multipleResultFunc);lua.set_function("variaArgs", variaArgs);// load and execute from filelua.script_file("test_sol2.lua");
test_sol2.lua內(nèi)容
obj = bark.my_class.new()
obj:g()
local result =obj:f()
obj:setTitle("I am Tom , and you ?")
print(">>>>>>")
obj:print()bark.print_my_class(obj)
print(result)print(bark.add_two_number(1,2))mr, mr2, mr3 = multipleResultFunc(11, "Jerry", 123.45)
print(mr, mr2, mr3)
print(mr)print("call variable args function", 1, 2, 3, 4)
local r= variaArgs(1,2,3,4)
print(variaArgs, r)
程序輸出
=== namespacing ===
I am Tom , and you ? , 25, class std::vector<unsigned long,class std::allocator<unsigned long> >
my_class { b: 25 }
24
3
11 Jerry 123.45
11
call variable args function 1 2 3 4
function: 0102B030 10
C source output Your name is Michael, and 2323 years old.