我有一个.c和.so文件。我尝试使用以下编译命令:gcc main.c -ldl
。在那个.c文件中,我通过dlsym()
链接到.so文件。如何使用.so文件编译.c文件。
g++ -o prog prog.o -ldllname
g++ -o prog prog.o -L/path/to/my/library/folder -ldllname
这是基于您进一步的评论。首先保护您头文件的声明。
#ifndef HEADER_PROTECT
#define HEADER_PROTECT
---------- Here is the content of header
#endif
-g : for debug information
fPIC: for position independent code
$gcc -fPIC -g myfile
The following will create the shared object libmyfile.so
$gcc -shared -o libymyfile.so myfile.o
Now,In order to link it with your main.c.
I assume that the libmyfile.so is in your current path, thus -L./
$gcc main.c -o main.out -L./ -lmyfile
Now, you need to export the LD_LIBRARY_PATH on the bash; in order to execute the binary.
$LD_LIBRARAY_PATH=$LD_LIBRARAY_PATH:./
$./main.out
dlsym 是在运行时从共享对象中加载符号的函数。如果您想在运行时加载共享对象,可以使用它。以下是 dlsym 的一个示例 破解库中的标准函数并在之后调用本地库函数。
dlsym()
用于在打开的库文件中查找符号。你需要先使用 dlopen()
打开文件,然后再使用 dlsym()
。