当前位置:网站首页>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=.
边栏推荐
- Mysql_12 多表查询
- Software Testing Interview Questions: Qualifying Criteria for Software Acceptance Testing?
- 2022 Hangzhou Electric Power Multi-School Session 3 Question L Two Permutations
- TinyMCE disable escape
- 关于我仔细检查审核过关于工作人员页面,返回一个所属行业问题
- canvas 高斯模糊效果
- MAUI Blazor 权限经验分享 (定位,使用相机)
- 软件测试面试题:什么是软件测试?软件测试的目的与原则?
- Will domestic websites use Hong Kong servers be blocked?
- tensor.nozero(),面具,面具
猜你喜欢

进程间通信和线程间通信

node uses redis

Matlab uses plotting method for data simulation and simulation
![[FreeRTOS] FreeRTOS and stm32 built-in stack occupancy](/img/33/3177b4c3de34d4920d741fed7526ee.png)
[FreeRTOS] FreeRTOS and stm32 built-in stack occupancy

gorm joint table query - actual combat

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

oracle创建用户以后的权限问题

QSunSync Qiniu cloud file synchronization tool, batch upload

TinyMCE禁用转义

MongoDB搭建及基础操作
随机推荐
ORA-00257
RK3399平台开发系列讲解(内核调试篇)2.50、嵌入式产品启动速度优化
[FreeRTOS] FreeRTOS and stm32 built-in stack occupancy
NMS原理及其代码实现
简单的顺序结构程序(C语言)
软件测试面试题:网络七层协仪具体?
Software testing interview questions: What stages should a complete set of tests consist of?
tiup telemetry
Redis visual management software Redis Desktop Manager2022
软件测试面试题:测试生命周期,测试过程分为几个阶段,以及各阶段的含义及使用的方法?
SV class virtual method of polymorphism
tiup uninstall
Mysql_13 事务
Software Testing Interview Questions: What's the Key to a Good Test Plan?
canvas Gaussian blur effect
《WEB安全渗透测试》(28)Burp Collaborator-dnslog外带技术
The principle of NMS and its code realization
Software Testing Interview Questions: What's the Difference Between Manual Testing and Automated Testing?
【无标题】
E - Distance Sequence (前缀和优化dp