当前位置:网站首页>GCC: Shield dependencies between dynamic libraries
GCC: Shield dependencies between dynamic libraries
2022-08-05 00:44:00 【calm as a cloud】
Assume a program with the following dependencies:
m depends on a
a depends on b
//a.c#include void b();void a(){printf("Here is a call b\n");b(); //b.c#include void b(){printf("Here is b\n"); //m.c#include void a();int main(){a();return 0; GCC: Compile-time library path and runtime library path - Programmer Sought
It has been stated that this program should compile and link.
But there is a place that is not very convenient, that is, m only depends on a on the surface, but it needs to add the b that a depends on when compiling and linking, so that it can run.For a more complex program, it is necessary to constantly check the program to confirm what the libraries that need to be compiled and linked finally have, and it is inconvenient.
Then is it possible to put the dependencies of a into the compilation process of a:
gcc -fpic -shared a.c -o liba.so -lb -L .
gcc -o m m.c -la -L.
Because library a needs library b when connecting, but cannot find it, an error is reported:
/usr/bin/ld: warning: libb.so, needed by ./liba.so, not found (try using -rpathor -rpath-link)
/usr/bin/ld: ./liba.so: undefined reference to `b'
collect2: error: ld returned 1 exit status
ldd liba.so
linux-vdso.so.1 (0x00007ffecb1fd000)
libb.so => not found
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f2ba19ad000)
/lib64/ld-linux-x86-64.so.2 (0x00007f2ba1bb7000)
It can be seen that this method does not work.
This problem can be solved by -Wl,-rpath=
-WL: Indicates that the compiler will pass the following parameters to the linker ld
-rpath: The path used to specify the compile link and runtime dependencies
Therefore, when compiling a, you can add the b it depends on in the following way:
gcc -fpic -shared a.c -o liba.so -lb -Wl,-rpath=.
gcc -o m m.c -la -L .
Note: After the above compilation, an error may be reported at runtime:
./m: error while loading shared libraries: liba.so: cannot open shared object file: No such file or directory
The reason is that liba.so cannot be found at runtime
There are two ways to solve this problem:
1. By setting LD_LIBRARY_PATH
export LD_LIBRARY_PATH=./:$LD_LIBRARY_PATH
2. Also specify when compiling m -Wl,-rpath=, that is:
gcc -o m m.c -la -Wl,-rpath=.
边栏推荐
- Opencv——视频跳帧处理
- leetcode:266. 回文全排列
- 关于我仔细检查审核过关于工作人员页面,返回一个所属行业问题
- Software Testing Interview Questions: What do you think about software process improvement? Is there something that needs improvement in the enterprise you have worked for? What do you expect the idea
- 主库预警日志报错ORA-00270
- 二叉树[全解](C语言)
- gorm的Raw与scan
- 【Unity编译器扩展之进度条】
- 机器学习(公式推导与代码实现)--sklearn机器学习库
- Mysql_12 多表查询
猜你喜欢
随机推荐
Pytorch usage and tricks
D - I Hate Non-integer Number (选数的计数dp
tiup update
Mysql_12 多表查询
2022杭电多校第一场 1004 Ball
2022牛客多校训练第二场 H题 Take the Elevator
电赛必备技能___定时ADC+DMA+串口通信
软件测试面试题:负载测试、容量测试、强度测试的区别?
Software Testing Interview Questions: What Are the Types of Software Testing?
软件测试面试题:什么是软件测试?软件测试的目的与原则?
2022 The Third J Question Journey
进程间通信和线程间通信
redis可视化管理软件Redis Desktop Manager2022
2022 Nioke Multi-School Training Session H Question H Take the Elevator
leetcode:269. 火星词典
MBps与Mbps区别
Software testing interview questions: Have you used some tools for software defect (Bug) management in your past software testing work? If so, please describe the process of software defect (Bug) trac
活动推荐 | 快手StreamLake品牌发布会,8月10日一起见证!
Mysql_13 事务
典型相关分析CCA计算过程









