欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

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

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

十余年的海晏網(wǎng)站建設經(jīng)驗,針對設計、前端、開發(fā)、售后、文案、推廣等六對一服務,響應快,48小時及時工作處理。網(wǎng)絡營銷推廣的優(yōu)勢是能夠根據(jù)用戶設備顯示端的尺寸不同,自動調(diào)整海晏建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設計,從而大程度地提升瀏覽體驗。成都創(chuàng)新互聯(lián)從事“海晏網(wǎng)站設計”,“海晏網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。

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)
{
    //壓棧后設置一個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)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。

網(wǎng)站欄目:Lua怎么調(diào)用C/C++函數(shù)/庫
文章鏈接:http://chinadenli.net/article44/gsggee.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供建站公司、網(wǎng)站設計、云服務器、外貿(mào)網(wǎng)站建設、域名注冊、企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運營