歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Lua入門之四:Lua調用C/C++庫(函數壓棧方式)

Lua入門之四:Lua調用C/C++庫(函數壓棧方式)

日期:2017/3/1 9:39:35   编辑:Linux編程

前面講過Lua載入dll的方式去調用函數庫,下面介紹的是函數壓棧的方式調用函數庫,通過lua_register把函數注冊到lua的棧中,lua_register的定義如下,

#define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n)))

看了定義就知道,其實就是函數壓棧,然後設置為全局變量,這樣lua就可以調用它了。

// libforlua-2.cpp : 定義控制台應用程序的入口點。
//

#include "stdafx.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <math.h>

//lua頭文件
#ifdef __cplusplus
extern "C" {
#include "lua.h"
#include <lauxlib.h>
#include <lualib.h>
}

#else

#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

#endif


#pragma comment(lib, "lua51.lib")


static int math_abs(lua_State *L)
{
//獲取傳入的參數
lua_pushnumber(L, abs((int)luaL_checknumber(L, 1)));

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 const luaL_reg mathlib[] = {
{ "abs", math_abs },
{ "cos", math_cos },
{ "sin", math_sin },
{ NULL, NULL }
};


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

int main(int argc, char** argv)
{
lua_State* L = lua_open();

luaL_openlibs(L);

// lua_register(L, "cos", math_cos);
// lua_register(L, "sin", math_sin);
// lua_register(L, "abs", math_abs);


//#define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n)))
luaL_register(L, "DY_MATH", mathlib);
char *luapath="./script/lua/lua.lua";
luaL_dofile(L, luapath);

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);


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);


//壓棧後設置一個lua可調用的全局函數名
lua_pushcfunction(L, ShowMessage);
lua_setglobal(L, "showmessage");
//c調用lua
lua_getglobal(L, "SHOWMESSAGE");
lua_pcall(L, 0, 0, 0);
printf("get the showmessage pushed value %f \n", lua_tonumber(L, -1));

lua_close(L);

}

---------------------------------------------

--region lua.lua
--Date
--此文件由[BabeLua]插件自動生成

function COS(a)
print("called COS in lua script")
--lua調用c/c++
return DY_MATH.cos(a)
end


function SIN(a)
print("called SIN in lua script")
return DY_MATH.sin(a)
end


function SHOWMESSAGE()
showmessage()
end
--endregion

Lua 語言 15 分鐘快速入門 http://www.linuxidc.com/Linux/2013-06/86582.htm

Lua程序設計(第2版)中文 PDF http://www.linuxidc.com/Linux/2013-03/81833.htm

Lua程序設計(第二版)閱讀筆記 http://www.linuxidc.com/Linux/2013-03/81834.htm

NetBSD 將支持用 Lua 腳本開發內核組件 http://www.linuxidc.com/Linux/2013-02/79527.htm

CentOS 編譯安裝 Lua LuaSocket http://www.linuxidc.com/Linux/2011-08/41105.htm

Lua 的詳細介紹:請點這裡
Lua 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved