当前位置:网站首页>GCC:屏蔽动态库之间的依赖
GCC:屏蔽动态库之间的依赖
2022-08-05 00:40:00 【风静如云】
假设有如下依赖关系的一个程序:
m 依赖于a
a 依赖于b
//a.c
#include <stdio.h>
void b();
void a()
{
printf("Here is a call b\n");
b();
//b.c
#include <stdio.h>
void b()
{
printf("Here is b\n");
//m.c
#include <stdio.h>
void a();
int main()
{
a();
return 0;
已经说明此程序应该编译连接。
但是这里有一个地方并不是十分的方便,就是m从表面上看只依赖a,却需要将a依赖的b在编译连接时也加入,方可运行。对于一个比较复杂的程序,那么就需要不断的查看程序,才能确认最终需要编译连接的库都有什么,及其的不方便。
那么可不可以将a的依赖需要放入a的编译过程中呢:
gcc -fpic -shared a.c -o liba.so -lb -L .
gcc -o m m.c -la -L.
由于连接时库a需要库b,却无法找到,因此报错:
/usr/bin/ld: warning: libb.so, needed by ./liba.so, not found (try using -rpath or -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)
可见这种方式行不通。
解决这个问题可以通过-Wl,-rpath=
-WL:表示编译器将后面的参数传递给链接器ld
-rpath:用于指定编译连接和运行时依赖的路径
因此编译a时可以通过如下方式,将其依赖的b加入:
gcc -fpic -shared a.c -o liba.so -lb -Wl,-rpath=.
gcc -o m m.c -la -L .
注:以上编译后,运行时可能会报错:
./m: error while loading shared libraries: liba.so: cannot open shared object file: No such file or directory
其原因是在运行时无法找到liba.so
解决这个问题有两个方法:
1.通过设置LD_LIBRARY_PATH
export LD_LIBRARY_PATH=./:$LD_LIBRARY_PATH
2.编译m时也指定 -Wl,-rpath=,即:
gcc -o m m.c -la -Wl,-rpath=.
边栏推荐
猜你喜欢

MongoDB construction and basic operations

2 用D435i运行VINS-fusion

TinyMCE禁用转义

OPENWIFI实践1:下载并编译SDRPi的HDL源码

oracle创建表空间

怎样进行在不改变主线程执行的时候,进行日志的记录

《WEB安全渗透测试》(28)Burp Collaborator-dnslog外带技术

Matlab uses plotting method for data simulation and simulation
![[230]连接Redis后执行命令错误 MISCONF Redis is configured to save RDB snapshots](/img/fa/5bdc81b1ebfc22d31f42da34427f3e.png)
[230]连接Redis后执行命令错误 MISCONF Redis is configured to save RDB snapshots

简单的顺序结构程序(C语言)
随机推荐
2 用D435i运行VINS-fusion
gorm的Raw与scan
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
D - I Hate Non-integer Number (count of selected number dp
ORA-00257
"No title"
E - Many Operations (按位考虑 + dp思想记录操作后的结果
BC(转)[js]js计算两个时间相差天数
leetcode: 267. Palindromic permutations II
软件测试面试题:软件测试类型都有哪些?
More than 2022 cattle school training topic Link with the second L Level Editor I
uinty lua 关于异步函数的终极思想
node uses redis
Software Testing Interview Questions: About Automated Testing Tools?
tiup status
2022 Hangzhou Electric Power Multi-School Session 3 Question B Boss Rush
软件测试面试题:系统测试的策略有?
2022多校第二场 K题 Link with Bracket Sequence I
leetcode:269. 火星词典
Software Testing Interview Questions: What aspects should be considered when designing test cases, i.e. what aspects should different test cases test against?