当前位置:网站首页>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
边栏推荐
- leetcode: 267. Palindromic permutations II
- LiveVideoStackCon 2022 Shanghai Station opens tomorrow!
- Opencv——视频跳帧处理
- Software testing interview questions: What are the three modules of LoadRunner?
- tensor.nozero(),面具,面具
- [idea] idea configures sql formatting
- Matlab uses plotting method for data simulation and simulation
- ora-01105 ora-03175
- 软件测试面试题:黑盒测试、白盒测试以及单元测试、集成测试、系统测试、验收测试的区别与联系?
- 【无标题】
猜你喜欢
随机推荐
The principle of NMS and its code realization
Software Testing Interview Questions: Qualifying Criteria for Software Acceptance Testing?
JUC线程池(一): FutureTask使用
oracle create tablespace
[230] Execute command error after connecting to Redis MISCONF Redis is configured to save RDB snapshots
LiveVideoStackCon 2022 上海站明日开幕!
仅3w报价B站up主竟带来1200w播放!品牌高性价比B站投放标杆!
E - Many Operations (bitwise consideration + dp thought to record the result after the operation
leetcode:269. 火星词典
创意代码表白
进程间通信和线程间通信
gorm联表查询-实战
tiup update
D - I Hate Non-integer Number (count of selected number dp
面试汇总:为何大厂面试官总问 Framework 的底层原理?
NMS原理及其代码实现
matlab 采用描点法进行数据模拟和仿真
软件测试面试题:您以往所从事的软件测试工作中,是否使用了一些工具来进行软件缺陷(Bug)的管理?如果有,请结合该工具描述软件缺陷(Bug)跟踪管理的流程?
If capturable=False, state_steps should not be CUDA tensors
E - Many Operations (按位考虑 + dp思想记录操作后的结果