一.先生成一个共享so库文件
// example.c
#include <stdio.h>
void hello() {
printf("Hello from the shared library!\n");
}
void test(int a)
{
printf("Test from the shared library! parameter is %d\n",a);
}
用命令生成so库文件
#编译共享库:
gcc -shared -fPIC -o libexample.so example.c
就会在目录下生成一个libexample.so文件
二.在主方法中执行该动态so库调用方法
int main() {
// ---- 加载动态so库文件
/**
* void* dlopen(const char* filename, int flag);
filename:要加载的共享库的路径(可以是绝对路径或相对路径)。如果值为 NULL,表示加载主程序(通常不使用该选项)。
flag:用于指定加载共享库的方式,可以是以下的一个或多个标志的组合:
RTLD_LAZY:延迟加载,即只有在调用符号时才进行解析。
RTLD_NOW:立即加载,即在加载时解析所有符号。
RTLD_GLOBAL:使库中的符号在其他共享库中可见(默认是 RTLD_LOCAL,即符号仅在当前库内部可见)。
RTLD_LOCAL:符号只对当前共享库可见。
*/
void* handle = dlopen("/demo_c/libexample.so", RTLD_LAZY);
/**
* 返回值:
成功时,dlopen 返回一个非 NULL 的指针,指向已加载的共享库的句柄。


1135

被折叠的 条评论
为什么被折叠?



