zoukankan      html  css  js  c++  java
  • lua中调用C++函数

    lua中调用C++函数

    我们产品中提供了很多lua-C API给用户在lua中调用,之前一直没用深究其实现原理,只是根据已有的代码在编码。显然这不是一个好的习惯,没用达到知其所以然的目的。

    一、基本原理

    将C++函数编译成动态链接库,然后在lua中require,通过下面的示例进行详解。

    #ifdef __cplusplus
    extern "C" {
    #endif
        #include<lua.h>
        #include<lualib.h>
        #include<lauxlib.h>
    #ifdef __cplusplus
    }
    #endif
    
    
    static int add (lua_State *L)
    #ifdef __cplusplus
    extern "C" {
    #endif
        #include<lua.h>
        #include<lualib.h>
        #include<lauxlib.h>
    #ifdef __cplusplus
    }
    #endif
    /**
        要写一个lua中可以调用的函数,一般需要三个步骤,
        第一步:定义函数,而且要遵守一定的规则,即
        参数只能有一个(lua_State*),返回值也只能为
        整型int,本例中static关键字可以不写。
    */
    static int add (lua_State *L)
    {
        double op1 = lua_tonumber(L, -1);
        double op2 = lua_tonumber(L, -2);
        lua_pushnumber(L, op1+op2);
        return 1;
    }
    
    /**
        第二步:列出所有的本模块中供lua调用的函数,为下
        面的注册做准备
    */
    static luaL_Reg myfuncs[] = {
        {"add", add},
        {NULL, NULL}
    };
    
    /**
        第三步:注册函数到一个lua table中去。
    */
    extern "C" int luaopen_mytestlib(lua_State *L)
    {
        luaL_register(L, "mytestlib", myfuncs);
        return 1;
    }
    

    二、编译

    $ g++ lua_call_cplusplus_function.cc -o mytestlib.so -shared -fPIC -I /usr/include/lua5.1/
    

    三、运行结果

    $ lua
    Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
    > require("mytestlib")
    > print(mytestlib.add(1,2))
    3
    

    四、注意事项

    • luaL_register在lua5.2中被移除了,如果使用lua5.3编译会报如下错误
    $ g++ mytestlib.cc -o test.so -shared -fPIC -I /home/wuman/下载/lua-5.3.2/src
    mytestlib.cc: In function ‘int luaopen_test(lua_State*)’:
    mytestlib.cc:21:34: error: ‘luaL_register’ was not declared in this scope
      luaL_register(L, "test", testlib);
    
    • luaopen_mytestlib这个 extern "C"是必须的,否则require时会报错:
    $ lua
    Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
    > require("mytestlib")
    error loading module 'mytestlib' from file './mytestlib.so':
    	./mytestlib.so: undefined symbol: luaopen_mytestlib
    stack traceback:
    	[C]: ?
    	[C]: in function 'require'
    	stdin:1: in main chunk
    	[C]: ?
    
  • 相关阅读:
    tensorflow slim代码使用
    Tensorflow学习之TF-Slim的使用
    FCN用卷积层代替FC层原因(转)
    ubuntu命令查看英伟达显卡型号
    传输
    将tf-faster-rcnn检测结果画在一张图像内
    GPU跑tf-faster-rcnn demo以及训练自己的数据
    以太网适配器的驱动程序出现问题
    TensofFlow函数: tf.image.crop_and_resize
    TensorFlow函数: tf.stop_gradient
  • 原文地址:https://www.cnblogs.com/keviwu/p/5877093.html
Copyright © 2011-2022 走看看