当前位置:网站首页>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杭电多校第三场 L题 Two Permutations
Redis visual management software Redis Desktop Manager2022
数据类型-整型(C语言)
日志(logging模块)
怎样进行在不改变主线程执行的时候,进行日志的记录
Software Testing Interview Questions: What's the Difference Between Manual Testing and Automated Testing?
oracle create user
The method of freely controlling concurrency in the sync package in GO
tiup update
leetcode:266. 回文全排列
软件测试面试题:手工测试与自动测试有哪些区别?
Software Testing Interview Questions: What is Software Testing?The purpose and principle of software testing?
阶段性测试完成后,你进行缺陷分析了么?
2022 Hangzhou Electric Power Multi-School Session 3 Question L Two Permutations
【FreeRTOS】FreeRTOS与stm32内置堆栈的占用情况
B站7月榜单丨飞瓜数据B站UP主排行榜发布!
Software Testing Interview Questions: What aspects should be considered when designing test cases, i.e. what aspects should different test cases test against?
克服项目管理中恐惧心理
Software testing interview questions: the difference and connection between black box testing, white box testing, and unit testing, integration testing, system testing, and acceptance testing?
typeScript - Partially apply a function









