当前位置:网站首页>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;
}
编译动态库liba.so和libb.so:
gcc -fpic -shared a.c -o liba.so
gcc -fpic -shared b.c -o libb.so
编译并连接程序m可能会遇到以下问题:
1.直接编译连接m
gcc -o m m.c
因为无法找到m直接依赖的动态库liba.so中定义的函数a,所以报错:
/usr/bin/ld: /tmp/ccpoXOmH.o: in function `main':
m.c:(.text+0xe): undefined reference to `a'
collect2: error: ld returned 1 exit status
2.编译指定动态库a和b:
gcc -o m m.c -la -lb
由于默认的库文件路径里没有包含当前目录,因此报错:
/usr/bin/ld: 找不到 -la
/usr/bin/ld: 找不到 -lb
collect2: error: ld returned 1 exit status
可以通过两种方式解决:
- 通过编译时参数-L,指定在当前目录下寻找库文件
gcc -o m m.c -la -lb -L .
- 通过设置LIBRARY_PATH
export LIBRARY_PATH=./:$LIBRARY_PATH
gcc -o m m.c -la -lb
3.运行期错误:
./m
./m: error while loading shared libraries: liba.so: cannot open shared object file: No such file or directory
这是由于在程序m加载时,无法在运行期连接库,因此报错。
可以通过设置LD_LIBRARY_PATH,解决运行期连接错误:
export LD_LIBRARY_PATH=./:$LD_LIBRARY_PATH
./m
Here is a call b
Here is b
边栏推荐
- tiup uninstall
- Countdown to 1 day!From August 2nd to 4th, I will talk with you about open source and employment!
- could not build server_names_hash, you should increase server_names_hash_bucket_size: 32
- tiup update
- matlab 采用描点法进行数据模拟和仿真
- node使用redis
- 2022 Hangzhou Electric Power Multi-School Session 3 Question B Boss Rush
- 阅读笔记:如何理解DevOps?
- leetcode:269. 火星词典
- OPENWIFI实践1:下载并编译SDRPi的HDL源码
猜你喜欢
redis可视化管理软件Redis Desktop Manager2022
TinyMCE disable escape
leetcode: 266. All Palindromic Permutations
英特尔WiFi 7产品将于2024年亮相 最高速度可达5.8Gbps
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
matlab中rcosdesign函数升余弦滚降成型滤波器
TinyMCE禁用转义
元宇宙:未来我们的每一个日常行为是否都能成为赚钱工具?
数据类型-整型(C语言)
随机推荐
Mysql_12 多表查询
Software Testing Interview Questions: What Are the Types of Software Testing?
2022杭电多校第三场 L题 Two Permutations
leetcode:266. 回文全排列
MAUI Blazor 权限经验分享 (定位,使用相机)
主库预警日志报错ORA-00270
could not build server_names_hash, you should increase server_names_hash_bucket_size: 32
tiup telemetry
软件测试面试题:软件都有多少种分类?
国内网站用香港服务器会被封吗?
仅3w报价B站up主竟带来1200w播放!品牌高性价比B站投放标杆!
Software testing interview questions: What are the three modules of LoadRunner?
如何用 Solidity 创建一个“Hello World”智能合约
Software Testing Interview Questions: What's the Key to a Good Test Plan?
【Unity编译器扩展之进度条】
软件测试面试题:负载测试、容量测试、强度测试的区别?
tiup status
阅读笔记:如何理解DevOps?
Software testing interview questions: How many types of software are there?
软件测试面试题:网络七层协仪具体?