溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Lua怎么調(diào)用C/C++函數(shù)/庫

發(fā)布時間:2021-10-14 10:51:01 來源:億速云 閱讀:208 作者:柒染 欄目:編程語言

Lua怎么調(diào)用C/C++函數(shù)/庫,針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

test.cpp文件

/*Lua調(diào)用C/C++函數(shù)/庫(函數(shù)壓棧方式)*/
#include<iostream>
using namespace std;
#include<lua.hpp>

/*
當我們需要在Lua里面調(diào)用C/C++函數(shù)時,所有的函數(shù)都必須滿足以下函數(shù)簽名:
typedef int (*lua_CFunction) (lua_State *L);換句話說,所有的函數(shù)必須接收一個lua_State作為參數(shù),同時返回一個整數(shù)值。因為這個函數(shù)使用Lua棧作為參數(shù),所以它可以從棧里面讀取任意數(shù)量和任意類型的參數(shù)。而這個函數(shù)的返回值則表示函數(shù)返回時有多少返回值被壓入Lua棧。(因為Lua的函數(shù)是可以返回多個值的)
*/
static int math_abs(lua_State *L)
{   
    lua_pushnumber(L, abs((int)luaL_checknumber(L, 1))); //獲取傳入的參數(shù)
    return 1;
}

static int math_cos(lua_State *L)
{
    lua_pushnumber(L, cos((double)luaL_checknumber(L, 1)));
    return 1;
}

static int math_sin(lua_State *L)
{
    lua_pushnumber(L, sin((double)luaL_checknumber(L, 1)));
    return 1;
}

static int ShowMessage(lua_State * L)
{
    lua_pushnumber(L, 1000);
    printf("show message and push 1000 \n");
    return -1;
}

//注冊函數(shù)
void regist_function(lua_State *L)
{
    //壓棧后設(shè)置一個lua可調(diào)用的全局函數(shù)名
    lua_pushcfunction(L, ShowMessage);
    lua_setglobal(L, "showmessage");
    //c調(diào)用lua
    lua_getglobal(L, "SHOWMESSAGE");
    lua_pcall(L, 0, 0, 0);
    printf("get the showmessage pushed value %f \n", lua_tonumber(L, -1));

    //#define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n)))
    //lua_register的定義如上,所有 lua_pushcfunction(L, ShowMessage);lua_setglobal(L, "showmessage"); <==>lua_register(L, "showmessage", ShowMessage);
    lua_register(L, "cos", math_cos);
    //測試
    lua_getglobal(L, "COS");
    lua_pushnumber(L, 0.5);
    if (0 != lua_pcall(L, 1, 1, 0))
    {
        printf("cpp call lua function failed\n");
    }
    printf("cos(0.5)=%f\n", lua_tonumber(L, -1));
    lua_pop(L, 1);
}
//注冊庫函數(shù)
void regist_lib(lua_State *L)
{
    static const luaL_reg mathlib[] = {
        { "abs", math_abs },
        { "cos", math_cos },
        { "sin", math_sin },
        { NULL, NULL }
    };
    luaL_register(L, "DY_MATH", mathlib);

    //測試
    double sinv = 30*3.1415926/180.0;
    lua_getglobal(L, "SIN");
    lua_pushnumber(L, sinv);
    if (0 != lua_pcall(L, 1, 1, 0))
    {
        printf("cpp call lua function failed\n");
    }
    printf("sin(%f)=%f\n", sinv, lua_tonumber(L, -1));
    lua_pop(L, 1);
}

int main()
{
    lua_State *L = luaL_newstate();
    luaL_openlibs(L);

    char *luapath="LuaCallCTest.lua";
    luaL_dofile(L, luapath);    
    
    regist_function(L);
    regist_lib(L);

    lua_close(L);
    system("pause");
    return 0;
}

LuaCallCTest.lua文件

--region LuaCallCTest.lua

function COS(a)
    print("called COS in lua script")
    --lua調(diào)用c/c++函數(shù)
    return cos(a)
end

function SIN(a)
    print("called SIN in lua script")
    --lua調(diào)用c/c++庫函數(shù)
    return DY_MATH.sin(a)
end

function SHOWMESSAGE()
    showmessage()
end

--end region

結(jié)果
Lua怎么調(diào)用C/C++函數(shù)/庫

關(guān)于Lua怎么調(diào)用C/C++函數(shù)/庫問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI