当前位置:网站首页>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=.
边栏推荐
- 仅3w报价B站up主竟带来1200w播放!品牌高性价比B站投放标杆!
- leetcode: 269. The Martian Dictionary
- 2022杭电多校第一场 1004 Ball
- More than 2022 cattle school training topic Link with the second L Level Editor I
- what?测试/开发程序员要被淘汰了?年龄40被砍到了32?一瞬间,有点缓不过神来......
- 软件测试面试题:软件验收测试的合格通过准则?
- oracle创建表空间
- Countdown to 1 day!From August 2nd to 4th, I will talk with you about open source and employment!
- uinty lua 关于异步函数的终极思想
- Software testing interview questions: What stages should a complete set of tests consist of?
猜你喜欢
oracle创建用户
Matlab uses plotting method for data simulation and simulation
国内网站用香港服务器会被封吗?
matlab中rcosdesign函数升余弦滚降成型滤波器
Mysql_13 事务
电子行业MES管理系统的主要功能与用途
【FreeRTOS】FreeRTOS与stm32内置堆栈的占用情况
Theory of Software Fundamentals
《MySQL入门很轻松》第2章:MySQL管理工具介绍
2022 Hangzhou Electric Power Multi-School Session 3 K Question Taxi
随机推荐
tiup status
D - I Hate Non-integer Number (count of selected number dp
"WEB Security Penetration Testing" (28) Burp Collaborator-dnslog out-band technology
Software testing interview questions: test life cycle, the test process is divided into several stages, and the meaning of each stage and the method used?
"No title"
oracle创建表空间
leetcode: 267. Palindromic permutations II
2022 Hangzhou Electric Multi-School 1004 Ball
2022 Hangzhou Electric Power Multi-School Session 3 Question L Two Permutations
软件测试面试题:请你分别画出 OSI 的七层网络结构图和 TCP/IP 的四层结构图?
软件测试面试题:负载测试、容量测试、强度测试的区别?
Zombie and orphan processes
2022牛客多校第三场 J题 Journey
简单的顺序结构程序(C语言)
tiup update
BC(转)[js]js计算两个时间相差天数
gorm joint table query - actual combat
仅3w报价B站up主竟带来1200w播放!品牌高性价比B站投放标杆!
00、数组及字符串常用的 API(详细剖析)
【unity编译器扩展之模型动画拷贝】