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.

目录
相关文章
|
索引 存储
lua 初接触 --- The first time use Lua for programing
  The first time use Lua for programing  Wang Xiao      1. 关于 lua 的变量类型:     lua 变量的定义与matlab有点不同:         local d , f = 5 ,10 --声明局部变量 d,f。
|
SQL Oracle 关系型数据库
|
7月前
|
存储 NoSQL Redis
Redis的Lua脚本有什么作用?
Redis Lua脚本用于减少网络开销、实现原子操作及扩展指令集。它能合并操作降低网络延迟,保证原子性,替代不支持回滚的事务。通过脚本,代码复用率提高,且可自定义指令,如实现分布式锁,增强Redis功能和灵活性。
254 1
|
6月前
|
消息中间件 NoSQL Java
Redis系列学习文章分享---第六篇(Redis实战篇--Redis分布式锁+实现思路+误删问题+原子性+lua脚本+Redisson功能介绍+可重入锁+WatchDog机制+multiLock)
Redis系列学习文章分享---第六篇(Redis实战篇--Redis分布式锁+实现思路+误删问题+原子性+lua脚本+Redisson功能介绍+可重入锁+WatchDog机制+multiLock)
229 0
|
2月前
|
缓存 分布式计算 NoSQL
大数据-43 Redis 功能扩展 Lua 脚本 对Redis扩展 eval redis.call redis.pcall
大数据-43 Redis 功能扩展 Lua 脚本 对Redis扩展 eval redis.call redis.pcall
31 2
|
3月前
|
存储 JSON Ubuntu
如何使用 Lua 脚本进行更复杂的网络请求,比如 POST 请求?
如何使用 Lua 脚本进行更复杂的网络请求,比如 POST 请求?
|
4月前
|
存储 NoSQL Redis
Tair的发展问题之在Redis集群模式下,Lua脚本操作key面临什么问题,如何解决
Tair的发展问题之在Redis集群模式下,Lua脚本操作key面临什么问题,如何解决
下一篇
无影云桌面