当前位置:网站首页>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
边栏推荐
- 2022 Hangzhou Electric Power Multi-School Session 3 Question L Two Permutations
- oracle create user
- oracle create tablespace
- 2022 Nioke Multi-School Training Session H Question H Take the Elevator
- Mysql_14 存储引擎
- E - Many Operations (bitwise consideration + dp thought to record the result after the operation
- Software Testing Interview Questions: What do test cases usually include?
- ARC129E Yet Another Minimization 题解 【网络流笔记】
- 2022杭电多校 第三场 B题 Boss Rush
- gorm的Raw与scan
猜你喜欢
Theory of Software Fundamentals
OPENWIFI实践1:下载并编译SDRPi的HDL源码
2 用D435i运行VINS-fusion
Redis visual management software Redis Desktop Manager2022
数据类型-整型(C语言)
could not build server_names_hash, you should increase server_names_hash_bucket_size: 32
Huggingface入门篇 II (QA)
2022 Hangzhou Electric Power Multi-School Session 3 K Question Taxi
SV class virtual method of polymorphism
could not build server_names_hash, you should increase server_names_hash_bucket_size: 32
随机推荐
[idea] idea configures sql formatting
what?测试/开发程序员要被淘汰了?年龄40被砍到了32?一瞬间,有点缓不过神来......
tiup status
标识符、关键字、常量 和变量(C语言)
D - I Hate Non-integer Number (count of selected number dp
uinty lua 关于异步函数的终极思想
ORA-00257
E - Distance Sequence (prefix and optimized dp
RK3399平台开发系列讲解(内核调试篇)2.50、嵌入式产品启动速度优化
2022 The Third J Question Journey
canvas Gaussian blur effect
Zombie and orphan processes
SV 类的虚方法 多态
Software testing interview questions: How many types of software are there?
软件测试面试题:您以往所从事的软件测试工作中,是否使用了一些工具来进行软件缺陷(Bug)的管理?如果有,请结合该工具描述软件缺陷(Bug)跟踪管理的流程?
"WEB Security Penetration Testing" (28) Burp Collaborator-dnslog out-band technology
2022杭电多校第三场 L题 Two Permutations
EL定时刷新页面中的皕杰报表实例
2022牛客多校训练第二场 J题 Link with Arithmetic Progression
tiup telemetry