Lua use package.loadlib or require link C code

Lua支持 通过动态链接库 加载C语言函数. 使用package.loadlib或require.
检查你所运行的环境是否支持.
> print(package.loadlib("a","b"))
nil     a: cannot open shared object file: No such file or directory    open

package.loadlib函数输出一个lua匿名函数. 如果加载不成功, 返回nil和错误消息.
上面的消息表示系统支持加载动态链接库, 只是没找到库文件而已.
使用例子 : 
创建一个测试C文件, 函数display
[root@db-172-16-3-150 ~]# vi a.c
#include <stdio.h>
#include "a.h"

void display() {
  fprintf(stdout, "this is display function in a.c\n");
}

头文件
[root@db-172-16-3-150 ~]# vi a.h
void display();

生成动态链接库.
[root@db-172-16-3-150 ~]# gcc -O3 -Wall -Wextra -Werror -g -fPIC -c ./a.c -o a.o
[root@db-172-16-3-150 ~]# gcc -O3 -Wall -Wextra -Werror -g -shared a.o -o liba.so

在lua中加载这个动态链接库文件, display函数.
[root@db-172-16-3-150 ~]# lua
> file = "/root/liba.so"
> f = package.loadlib(file, "display")  -- 加载动态链接库中的C函数到lua函数
> print(f)
function: 0x7f593eb625b0
> f()  -- 直接调用这个lua函数.
this is display function in a.c


[参考]
1. 

package.loadlib (libname, funcname)

Dynamically links the host program with the C library libname.

If funcname is "*", then it only links with the library, making the symbols exported by the library available to other dynamically linked libraries. Otherwise, it looks for a function funcnameinside the library and returns this function as a C function. So, funcname must follow the lua_CFunction prototype (see lua_CFunction).

This is a low-level function. It completely bypasses the package and module system. Unlike require, it does not perform any path searching and does not automatically adds extensions.libname must be the complete file name of the C library, including if necessary a path and an extension. funcname must be the exact name exported by the C library (which may depend on the C compiler and linker used).

This function is not supported by Standard C. As such, it is only available on some platforms (Windows, Linux, Mac OS X, Solaris, BSD, plus other Unix systems that support the dlfcnstandard).

2. 

require (modname)

Loads the given module. The function starts by looking into the package.loaded table to determine whether modname is already loaded. If it is, then require returns the value stored atpackage.loaded[modname]. Otherwise, it tries to find a loader for the module.

To find a loader, require is guided by the package.searchers sequence. By changing this sequence, we can change how require looks for a module. The following explanation is based on the default configuration for package.searchers.

First require queries package.preload[modname]. If it has a value, this value (which should be a function) is the loader. Otherwise require searches for a Lua loader using the path stored in package.path. If that also fails, it searches for a C loader using the path stored in package.cpath. If that also fails, it tries an all-in-one loader (see package.searchers).

Once a loader is found, require calls the loader with two arguments: modname and an extra value dependent on how it got the loader. (If the loader came from a file, this extra value is the file name.) If the loader returns any non-nil value, require assigns the returned value to package.loaded[modname]. If the loader does not return a non-nil value and has not assigned any value to package.loaded[modname], then require assigns true to this entry. In any case, require returns the final value of package.loaded[modname].

If there is any error loading or running the module, or if it cannot find any loader for the module, then require raises an error.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值