lua三底漆:lua转让c/c++库(动态链接模式)
2015-07-16 21:46
393 查看
dll按功能luaL_openlib出口,然后lua使用package.loadlib导入库函数,基本就是这么个过程,以下上代码来说明一切。
babelua插件的设置:
演示样例project的下载地址:http://download.csdn.net/detail/x356982611/7401781
#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插件的设置:
演示样例project的下载地址:http://download.csdn.net/detail/x356982611/7401781
相关文章推荐
- lua语言实现面向对象编程
- lua和c的交互
- [Lua]Lua高级教程Metatables
- [LeetCode]Evaluate Reverse Polish Notation
- vs插件 BabelLua的使用
- Lua for学习
- lua 首次编译
- Lua查找表元素过程(元表、__index方法是如何工作的)
- lua动态链接库(luaopen_*函数的使用)
- [Lua]Mac系统上安装Lua环境
- lua-md5 for lua5.3
- Lua 运行
- 浅析Lua中table的遍历和删除
- Lua5.2.3源码阅读(2)-Table
- Lua5.2.3源码阅读(3)-Table(ipairs,pairs)
- Lua基础和Lua版飞机大战
- c++获取lua嵌套table某属性的值
- Lua5.2.3源码阅读(2)-Table
- [leetcode] 150.Evaluate Reverse Polish Notation
- Lua5.1.4代码分析(二十三)-如何实现Lua代码的热更新