Lua加载C库的问题

写了一个测试Lua调用的C库,如下:

头文件:

#ifndef __C_LIB_H_FILE__
#define __C_LIB_H_FILE__

extern "C"
{
  #include <lua.h>
  #include <lauxlib.h>
  #include <lualib.h>
  __attribute__ ((visibility("default")))
  int luaopen_libtestlib( lua_State *l );
}

//int mymax( lua_State * l );


#endif // __C_LIB_H_FILE__

cpp文件

#include "clib.h"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>


int mymax( lua_State * l )
{
  int a = lua_tointeger( l, 1 );
  int b = lua_tointeger( l, 2 );

  if ( a > b )
    {
      lua_pushinteger( l, a );
    }else
    {
      lua_pushinteger( l,  b );
    }

  return 1;
}

const struct luaL_Reg funcs[]=
  {
   {"mymax", mymax},
   { NULL, NULL}

  };

extern "C"
__attribute__ ((visibility("default")))
int luaopen_libtestlib( lua_State *l )
{
  luaL_register( l, "testlib", funcs );
  return 1;
}

然后编译,用的lua脚本如下:

function callc()
  local testlib = require("libtestlib")
 
  print ( testlib.mymax( 12, 6 ) )
end

print("C path:", package.cpath )
print("hello module")

callc()

调用方式:生成的库和lua脚本在同一个目录,调用方式如下:

lua usec.lua

然后就发现死活找不到写C函数库,后来使用assert()把rquire("libtestlib")包住,发现输出信息是无法找到链接库,为什么呢?

输出package.cpath,报错,说package是个nil值。

后来打开synaptic,发现安装的lua包是不完整的,找了个lua5.1安装后,才正确输出结果。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值