11
Linux下动态库(共享库)的制作与使用
https://blog.youkuaiyun.com/weixin_51483516/article/details/120621135
echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$(pwd)'
22
Linux 动态库和静态库
https://blog.youkuaiyun.com/qq_58724706/article/details/125185425
动态库调用静态库
http://blog.chinaunix.net/uid-9552208-id-4205210.html
生成动态库: 需要的目标文件得用-fPIC选项生成.
而静态库所需的目标文件可以不用-fPIC选项.
一个应用程序调用动态库, 而这个动态库其中的函数调用某静态库时,如何生成应用程序呢?
例:
/////// static.h
void static_print();
///////static.cpp
#include
#include “static.h”
void static_print() {
std::cout<<"This is static_print function"<
}
////// shared.h
void shared_print();
////// shared.cpp
#include
#include “shared.h”
#include “static.h”
void shared_print() {
std::cout<<"This is shared_print function";
static_print();
}
////////test.cpp
#include "share.h"
int main()
{
shared_print();
return 0;
}
方法一:
静态库的.o文件也用-fPIC生成. 生成动态库时把静态库加入.
生成应用程序时只加载动态库
g++ -c -fPIC static.cpp // 生成static.o
ar -r libstatic.a static.o // 生成静态库libstatic.a
g++ -c -fPIC shared.cpp // 生成shared.o
g++ -shared shared.o -lstatic -o libshared.so // 生成动态库libshared.so 注: -shared是g++的选项,与shared.o无关. -lstatic选项把libstatic.a的函数加入动态库中.
g++ test.cpp -lshared -o test.exe // link libshared.so 到test.exe中.
方法二:
静态库的.o文件不用-fPIC生成. 生成动态库时不加表态库.
生成应用程序时加载动态库和静态库.
g++ -c static.cpp // 生成static.o
ar -r libstatic.a static.o // 生成静态库libstatic.a
g++ -c -fPIC shared.cpp // 生成shared.o
g++ -shared shared.o -o libshared.so // 生成动态库libshared.so 注: -shared是g++的选项,与shared.o无关. 这时如果加-lstatic. error:relocation R_X86_64_32 against `a local symbol' can not be used when making a shared object; recompile with -fPIC
g++ test.cpp -lshared -lstatic -o test.exe // link libshared.so 到test.exe中.
两种方法的不同之处在于static_print的实际代码一个在.so中.一个在最后test.exe文件中. 个人觉得第一种方法更好, 因为动态库应该看成一个可以独立运行的程序.
查看 动态链接库的格式:
使用file 命令
file libMyTest.so
libMyTest.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=03289e3a7dac440c9a1200705fd3093d03ae2ed9, not stripped
LINUX下动态链接库的使用(dlopen/dlsym/dlclose/dlerror)
https://blog.youkuaiyun.com/wteruiycbqqvwt/article/details/103584438
begin to dlopen
dlopen fail
xxxDll.so: invalid ELF header
遇到问题
/usr/bin/ld: xxxDll.a(xxxDll.o): relocation R_X86_64_PC32 against symbol `xxxxbuf’ can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: bad value
collect2: error: ld returned 1 exit status
1万+

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



