lua三底漆:lua转让c/c++库(动态链接模式)

dll按功能luaL_openlib出口,然后lua使用package.loadlib导入库函数,基本就是这么个过程,以下上代码来说明一切。



#include "stdafx.h"

#ifdef __cplusplus
extern "C"{
#endif #include "lua.h"
#include "lualib.h"
#include "lauxlib.h" #ifdef __cplusplus
}
#endif #include <math.h> #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;
} #ifdef _WIN32
extern "C" __declspec(dllexport) int luaopen_luadlllib(lua_State* L)
{
#else
extern "C" int luaopen_luadlllib(lua_State* L)
{ #endif // _WIN32 //MessageBox(NULL, TEXT("Register C++ Functions..."), NULL, MB_OK); luaL_openlib(L, "DY_MATH", mathlib,0);
return 1;
}
--region loadlib.lua
--Date
--此文件由[BabeLua]插件自己主动生成 --(package.loadlib("./../Debug/libforlua", "luaopen_luadlllib"))()
--(package.loadlib("./../Debug/libforlua.dll", "luaopen_luadlllib"))()
local libpath="./../Debug/libforlua.dll"
local loadlibfunc=package.loadlib(libpath,"luaopen_luadlllib")
loadlibfunc() function COS(a)
print("called COS in lua script")
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 print(COS(60*3.1415926/180)) print("enter a number:")
a = io.read("*number") --endregion

babelua插件的设置:

lua三底漆:lua转让c/c++库(动态链接模式)

演示样例project的下载地址:http://download.****.net/detail/x356982611/7401781

版权声明:本文博客原创文章,博客,未经同意,不得转载。

上一篇:学习笔记-AngularJs(七)


下一篇:java 线程演示