当前位置:网站首页>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=.
边栏推荐
- [230] Execute command error after connecting to Redis MISCONF Redis is configured to save RDB snapshots
- Software testing interview questions: the difference and connection between black box testing, white box testing, and unit testing, integration testing, system testing, and acceptance testing?
- could not build server_names_hash, you should increase server_names_hash_bucket_size: 32
- "WEB Security Penetration Testing" (28) Burp Collaborator-dnslog out-band technology
- Mysql_14 存储引擎
- 网站最终产品页使用单一入口还是多入口?
- 2022 Hangzhou Electric Power Multi-School Session 3 K Question Taxi
- B站7月榜单丨飞瓜数据B站UP主排行榜发布!
- E - Distance Sequence (prefix and optimized dp
- TinyMCE禁用转义
猜你喜欢

机器学习(公式推导与代码实现)--sklearn机器学习库

Theory of Software Fundamentals

node uses redis

英特尔WiFi 7产品将于2024年亮相 最高速度可达5.8Gbps

gorm joint table query - actual combat

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

【idea】idea配置sql格式化

典型相关分析CCA计算过程

could not build server_names_hash, you should increase server_names_hash_bucket_size: 32

Inter-process communication and inter-thread communication
随机推荐
leetcode:269. 火星词典
Mysql_12 多表查询
Mysql_14 存储引擎
电赛必备技能___定时ADC+DMA+串口通信
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
2022 Hangzhou Electric Power Multi-School Session 3 K Question Taxi
node使用redis
tensor.nozero(),面具,面具
[FreeRTOS] FreeRTOS and stm32 built-in stack occupancy
软件测试面试题:测试生命周期,测试过程分为几个阶段,以及各阶段的含义及使用的方法?
元宇宙:未来我们的每一个日常行为是否都能成为赚钱工具?
oracle创建用户
matlab 采用描点法进行数据模拟和仿真
Will domestic websites use Hong Kong servers be blocked?
2022杭电多校 第三场 B题 Boss Rush
2022多校第二场 K题 Link with Bracket Sequence I
Countdown to 1 day!From August 2nd to 4th, I will talk with you about open source and employment!
2022牛客多校第三场 J题 Journey
Software testing interview questions: What are the strategies for system testing?
Theory of Software Fundamentals