当前位置:网站首页>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
边栏推荐
- 软件测试面试题:请你分别画出 OSI 的七层网络结构图和 TCP/IP 的四层结构图?
- leetcode: 269. The Martian Dictionary
- 关于我仔细检查审核过关于工作人员页面,返回一个所属行业问题
- E - Many Operations (bitwise consideration + dp thought to record the result after the operation
- 倒计时1天!8月2日—4日与你聊聊开源与就业那些事!
- matlab 采用描点法进行数据模拟和仿真
- ORA-00257
- E - Many Operations (按位考虑 + dp思想记录操作后的结果
- B站7月榜单丨飞瓜数据B站UP主排行榜发布!
- 工具类总结
猜你喜欢
node uses redis
If capturable=False, state_steps should not be CUDA tensors
仅3w报价B站up主竟带来1200w播放!品牌高性价比B站投放标杆!
[FreeRTOS] FreeRTOS and stm32 built-in stack occupancy
【FreeRTOS】FreeRTOS与stm32内置堆栈的占用情况
LiveVideoStackCon 2022 Shanghai Station opens tomorrow!
matlab 采用描点法进行数据模拟和仿真
软件基础的理论
could not build server_names_hash, you should increase server_names_hash_bucket_size: 32
If capturable=False, state_steps should not be CUDA tensors
随机推荐
[230]连接Redis后执行命令错误 MISCONF Redis is configured to save RDB snapshots
could not build server_names_hash, you should increase server_names_hash_bucket_size: 32
Software testing interview questions: What stages should a complete set of tests consist of?
ORA-01105 ORA-03175
2022杭电多校第三场 L题 Two Permutations
EL定时刷新页面中的皕杰报表实例
2022牛客多校训练第二场 J题 Link with Arithmetic Progression
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?
FSAWS 的全球基础设施和网络
【FreeRTOS】FreeRTOS与stm32内置堆栈的占用情况
僵尸进程和孤儿进程
软件测试面试题:软件验收测试的合格通过准则?
Software testing interview questions: Have you used some tools for software defect (Bug) management in your past software testing work? If so, please describe the process of software defect (Bug) trac
gorm联表查询-实战
[idea] idea configures sql formatting
Pytorch使用和技巧
2022 Hangzhou Electric Power Multi-School Session 3 Question L Two Permutations
软件测试面试题:系统测试的策略有?
Helm Chart
Software Testing Interview Questions: What do test cases usually include?