zoukankan      html  css  js  c++  java
  • 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 
    ");
    	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


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

  • 相关阅读:
    二分数组的一些搜索方法
    获取图像lbp特征
    字符串的模糊搜索
    Python numpy读取图片方法
    红方人员实战手册转载
    libuv的交叉编译
    Gogs的交叉编译与配置
    配置PHP8与Nginx并启动nextcloud
    hi3798mv100SDK上DropBear的交叉编译
    Nginx的交叉编译
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4652616.html
Copyright © 2011-2022 走看看