zoukankan      html  css  js  c++  java
  • lua编译出liblua.so

    偶遇一个事情,需要在C里面嵌入Lua代码,这真是痛苦了我好久….

    不知道为啥lua默认编译没有生成.so 的动态链接库,需要修改Makefile生成liblua.so,我用的版本是5.2

    一、先修改根目录的 Makefile
    修改一行

    TO_LIB= liblua.a liblua.so

    二、再修改src的Makefile
    注意,请搜索关键字,第二行是需要修改的,第一行,和第三行是粘贴的,至于粘贴的位置嘛,你看样子比较像的就粘贴一起吧
    第三行的开始是tab键

    LUA_SO=liblua.so
    ALL_T= $(LUA_A) $(LUA_T) $(LUAC_T) $(LUA_SO)
    $(LUA_SO): $(CORE_O) $(LIB_O)
    $(CC) -o $@ -shared $? -ldl -lm

    然后编译,安装,ldconfig

    make linux
    make install

    在/etc/ld.so.conf中加入一行
    /usr/local/lib/
    然后执行
    /sbin/ldconfig
    重新加载动态链接库。

    三、敲两段代码
    luadd.c

    #include 
    #include "lua.h"
    #include "lualib.h"
    #include "lauxlib.h"

    /* the Lua interpreter */
    lua_State* L;

    int luaadd ( int x, int y )
    {
    int sum;

    /* the function name */
    lua_getglobal(L,"add");

    /* the first argument */
    lua_pushnumber(L, x);

    /* the second argument */
    lua_pushnumber(L, y);

    /* call the function with 2
    arguments, return 1 result */
    lua_call(L, 2, 1);
    //lua_pcall(L, 2, 1,0);

    /* get the result */
    sum = (int)lua_tointeger(L, -1);
    lua_pop(L, 1);

    return sum;
    }

    int main ( int argc, char *argv[] )
    {
    int sum;

    /* initialize Lua */
    //L = lua_open();
    L = luaL_newstate();

    /* load Lua base libraries */
    //luaL_openlibs(L);
    luaopen_base(L);

    /* load the script */
    (void)luaL_dofile(L, "add.lua");

    /* call the add function */
    sum = luaadd( 10, 15 );

    /* print the result */
    printf( "The sum is %d ", sum );

    /* cleanup Lua */
    lua_close(L);

    /* pause */
    //printf( "Press enter to exit..." );
    //getchar();

    return 0;
    }

    lua代码 add.lua

    function add(x,y)
    return x+y
    end

    四、当然是gcc了

    gcc luadd.c -llua -ldl -g

    五、不出意外的话,当然是出现结果

    the sum is 25

    希望大家这过程顺利

  • 相关阅读:
    JS-窗体对象 与 事件返回值属性
    JS-事件流操作
    JS-鼠标、键盘事件及事件对象/event
    JS-事件
    JS-DOM样式操作
    JS-DOM节点属性
    AVS 通信模块之AVSConnectionManager
    AVS 通信模块
    AVS 通信协议
    AVS SampleApp
  • 原文地址:https://www.cnblogs.com/ghost240/p/3421020.html
Copyright © 2011-2022 走看看