当前位置:网站首页>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
边栏推荐
- ORA-01105 ORA-03175
- The method of freely controlling concurrency in the sync package in GO
- [230]连接Redis后执行命令错误 MISCONF Redis is configured to save RDB snapshots
- E - Distance Sequence (前缀和优化dp
- JUC线程池(一): FutureTask使用
- leetcode:267. 回文排列 II
- Software testing interview questions: What are the seven-layer network protocols?
- redis可视化管理软件Redis Desktop Manager2022
- tiup status
- 2022牛客多校第三场 A Ancestor
猜你喜欢
随机推荐
node使用redis
面试汇总:为何大厂面试官总问 Framework 的底层原理?
E - Distance Sequence (前缀和优化dp
D - I Hate Non-integer Number (count of selected number dp
LiveVideoStackCon 2022 Shanghai Station opens tomorrow!
FSAWS 的全球基础设施和网络
tensor.nozero(),面具,面具
canvas 高斯模糊效果
2022多校第二场 K题 Link with Bracket Sequence I
gorm joint table query - actual combat
TinyMCE disable escape
软件测试面试题:请你分别画出 OSI 的七层网络结构图和 TCP/IP 的四层结构图?
3. pcie.v file
2021年11月网络规划设计师上午题知识点(上)
tiup uninstall
tiup update
leetcode: 266. All Palindromic Permutations
If capturable=False, state_steps should not be CUDA tensors
倒计时1天!8月2日—4日与你聊聊开源与就业那些事!
软件测试面试题:测试用例通常包括那些内容?

![[idea] idea configures sql formatting](/img/89/98cd23aff3e2f15ecb489f8b3c50e9.png)




![[230] Execute command error after connecting to Redis MISCONF Redis is configured to save RDB snapshots](/img/fa/5bdc81b1ebfc22d31f42da34427f3e.png)

