当前位置:网站首页>GCC: compile-time library path and runtime library path
GCC: compile-time library path and runtime library path
2022-08-05 00:45:00 【calm as a cloud】
Assume a program with the following dependencies:
m depends on a
a depends on b
//a.c#include void b();void a(){printf("Here is a call b\n");b();}
//b.c#include void b(){printf("Here is b\n");}
//m.c#include void a();int main(){a();return 0;}
Compile the dynamic libraries liba.so and libb.so:
gcc -fpic -shared a.c -o liba.so
gcc -fpic -shared b.c -o libb.so
Compiling and linking program m may encounter the following problems:
1. Directly compile and link m
gcc -o m m.c
Because the function a defined in the dynamic library liba.so that m directly depends on cannot be found, an error is reported:
/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. Compile the specified dynamic libraries a and b:
gcc -o m m.c -la -lb
Since the default library file path does not contain the current directory, an error is reported:
/usr/bin/ld: cannot find -la
/usr/bin/ld: cannot find -lb
collect2: error: ld returned 1 exit status
It can be solved in two ways:
- Use the compile-time parameter -L to specify to find library files in the current directory
gcc -o m m.c -la -lb -L .
- By setting LIBRARY_PATH
export LIBRARY_PATH=./:$LIBRARY_PATH
gcc -o m m.c -la -lb
3. Runtime error:
./m
./m: error while loading shared libraries: liba.so: cannot open shared object file: No such file or directory
This is because the library cannot be connected at runtime when the program m is loaded, so an error is reported.
Runtime can be resolved by settingLD_LIBRARY_PATH,Connection error:
export LD_LIBRARY_PATH=./:$LD_LIBRARY_PATH
./m
Here is a call b
Here is b
边栏推荐
- ### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionExcep
- 软件基础的理论
- Software test interview questions: BIOS, Fat, IDE, Sata, SCSI, Ntfs windows NT?
- 软件测试面试题:系统测试的策略有?
- 如何用 Solidity 创建一个“Hello World”智能合约
- 2022牛客多校训练第二场 H题 Take the Elevator
- 软件测试面试题:设计测试用例时应该考虑哪些方面,即不同的测试用例针对那些方面进行测试?
- JUC线程池(一): FutureTask使用
- B站7月榜单丨飞瓜数据B站UP主排行榜发布!
- 2022牛客多校第三场 A Ancestor
猜你喜欢
Matlab uses plotting method for data simulation and simulation
TinyMCE disable escape
could not build server_names_hash, you should increase server_names_hash_bucket_size: 32
2022杭电多校第三场 K题 Taxi
Memory Forensics Series 1
ORA-01105 ORA-03175
动态规划/背包问题总结/小结——01背包、完全背包
MongoDB construction and basic operations
活动推荐 | 快手StreamLake品牌发布会,8月10日一起见证!
4. PCIe 接口时序
随机推荐
软件测试面试题:一套完整的测试应该由哪些阶段组成?
Software testing interview questions: Please draw the seven-layer network structure diagram of OSI and the four-layer structure diagram of TCP/IP?
5.PCIe官方示例
D - I Hate Non-integer Number (选数的计数dp
Matlab uses plotting method for data simulation and simulation
[230]连接Redis后执行命令错误 MISCONF Redis is configured to save RDB snapshots
BC(转)[js]js计算两个时间相差天数
Software testing interview questions: What is the difference between load testing, capacity testing, and strength testing?
Software Testing Interview Questions: What do test cases usually include?
2022杭电多校训练第三场 1009 Package Delivery
Software Testing Interview Questions: What is Software Testing?The purpose and principle of software testing?
could not build server_names_hash, you should increase server_names_hash_bucket_size: 32
[FreeRTOS] FreeRTOS and stm32 built-in stack occupancy
2021年11月网络规划设计师上午题知识点(下)
4. PCIe 接口时序
仅3w报价B站up主竟带来1200w播放!品牌高性价比B站投放标杆!
Binary tree [full solution] (C language)
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?
活动推荐 | 快手StreamLake品牌发布会,8月10日一起见证!
Pytorch使用和技巧