> print(package.loadlib("a","b"))
nil a: cannot open shared object file: No such file or directory open
[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
[参考]
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 funcname
inside 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 dlfcn
standard).
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.