当前位置:网站首页>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=.
边栏推荐
猜你喜欢
![[230] Execute command error after connecting to Redis MISCONF Redis is configured to save RDB snapshots](/img/fa/5bdc81b1ebfc22d31f42da34427f3e.png)
[230] Execute command error after connecting to Redis MISCONF Redis is configured to save RDB snapshots
![[idea] idea configures sql formatting](/img/89/98cd23aff3e2f15ecb489f8b3c50e9.png)
[idea] idea configures sql formatting
![[230]连接Redis后执行命令错误 MISCONF Redis is configured to save RDB snapshots](/img/fa/5bdc81b1ebfc22d31f42da34427f3e.png)
[230]连接Redis后执行命令错误 MISCONF Redis is configured to save RDB snapshots

LiveVideoStackCon 2022 上海站明日开幕!

2022 Hangzhou Electric Power Multi-School Session 3 K Question Taxi

CNI(Container Network Plugin)
](/img/4d/2d81dc75433c23c5ba6b31453396f0.png)
二叉树[全解](C语言)

SV class virtual method of polymorphism

QSunSync 七牛云文件同步工具,批量上传

could not build server_names_hash, you should increase server_names_hash_bucket_size: 32
随机推荐
oracle create tablespace
刘润直播预告 | 顶级高手,如何创造财富
00、数组及字符串常用的 API(详细剖析)
软件测试面试题:关于自动化测试工具?
5.PCIe官方示例
2022杭电多校第三场 K题 Taxi
leetcode:267. 回文排列 II
oracle create user
RK3399平台开发系列讲解(内核调试篇)2.50、嵌入式产品启动速度优化
软件测试面试题:系统测试的策略有?
软件基础的理论
oracle创建表空间
Countdown to 1 day!From August 2nd to 4th, I will talk with you about open source and employment!
Zombie and orphan processes
MBps与Mbps区别
阶段性测试完成后,你进行缺陷分析了么?
软件测试面试题:手工测试与自动测试有哪些区别?
2022 Hangzhou Electric Multi-School Training Session 3 1009 Package Delivery
TinyMCE禁用转义
2022多校第二场 K题 Link with Bracket Sequence I