当前位置:网站首页>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
边栏推荐
猜你喜欢

gorm joint table query - actual combat

电赛必备技能___定时ADC+DMA+串口通信

Huggingface入门篇 II (QA)

阶段性测试完成后,你进行缺陷分析了么?

oracle创建用户

仅3w报价B站up主竟带来1200w播放!品牌高性价比B站投放标杆!

could not build server_names_hash, you should increase server_names_hash_bucket_size: 32

MongoDB搭建及基础操作

could not build server_names_hash, you should increase server_names_hash_bucket_size: 32

电子行业MES管理系统的主要功能与用途
随机推荐
软件测试面试题:黑盒测试、白盒测试以及单元测试、集成测试、系统测试、验收测试的区别与联系?
阶段性测试完成后,你进行缺陷分析了么?
could not build server_names_hash, you should increase server_names_hash_bucket_size: 32
软件测试面试题:软件验收测试的合格通过准则?
国内网站用香港服务器会被封吗?
2022 Hangzhou Electric Multi-School 1004 Ball
标识符、关键字、常量 和变量(C语言)
Software Testing Interview Questions: What do you think about software process improvement? Is there something that needs improvement in the enterprise you have worked for? What do you expect the idea
MongoDB搭建及基础操作
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?
TinyMCE禁用转义
tiup update
Pytorch usage and tricks
Software testing interview questions: What are the strategies for system testing?
Software Testing Interview Questions: What aspects should be considered when designing test cases, i.e. what aspects should different test cases test against?
元宇宙:未来我们的每一个日常行为是否都能成为赚钱工具?
刘润直播预告 | 顶级高手,如何创造财富
Mysql_12 多表查询
Will domestic websites use Hong Kong servers be blocked?
数据类型-整型(C语言)