linux下so动态库一些不为人知的秘密(上)
linux 下有动态库和静态库,动态库以.so为扩展名,静态库以.a为扩展名。二者都使用广泛。本文主要讲动态库方面知识。
- # ldd /bin/ls
- linux-vdso.so.1 => (0x00007fff597ff000)
- libselinux.so.1 => /lib64/libselinux.so.1 (0x00000036c2e00000)
- librt.so.1 => /lib64/librt.so.1 (0x00000036c2200000)
- libcap.so.2 => /lib64/libcap.so.2 (0x00000036c4a00000)
- libacl.so.1 => /lib64/libacl.so.1 (0x00000036d0600000)
- libc.so.6 => /lib64/libc.so.6 (0x00000036c1200000)
- libdl.so.2 => /lib64/libdl.so.2 (0x00000036c1600000)
- /lib64/ld-linux-x86-64.so.2 (0x00000036c0e00000)
- libpthread.so.0 => /lib64/libpthread.so.0 (0x00000036c1a00000)
- libattr.so.1 => /lib64/libattr.so.1 (0x00000036cf600000)
这么多so,是的。使用ldd显示的so,并不是所有so都是需要使用的,下面举个例子
- #include <stdio.h>
- #include <iostream>
- #include <string>
- using namespace std;
- int main ()
- {
- cout << "test" << endl;
- return 0;
- }
- # g++ -o demo main.cpp
- # ldd demo
- linux-vdso.so.1 => (0x00007fffcd1ff000)
- libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x00007f4d02f69000)
- libm.so.6 => /lib64/libm.so.6 (0x00000036c1e00000)
- libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00000036c7e00000)
- libc.so.6 => /lib64/libc.so.6 (0x00000036c1200000)
- /lib64/ld-linux-x86-64.so.2 (0x00000036c0e00000)
- # g++ -o demo -lz -lm -lrt main.cpp
- # ldd demo
- linux-vdso.so.1 => (0x00007fff0f7fc000)
- libz.so.1 => /lib64/libz.so.1 (0x00000036c2600000)
- librt.so.1 => /lib64/librt.so.1 (0x00000036c2200000)
- libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x00007ff6ab70d000)
- libm.so.6 => /lib64/libm.so.6 (0x00000036c1e00000)
- libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00000036c7e00000)
- libc.so.6 => /lib64/libc.so.6 (0x00000036c1200000)
- libpthread.so.0 => /lib64/libpthread.so.0 (0x00000036c1a00000)
- /lib64/ld-linux-x86-64.so.2 (0x00000036c0e00000)
看看,虽然没有用到,但是一样有链接进来,那看看程序启动时候有没有去加载它们呢
- # strace ./demo
- execve("./demo", ["./demo"], [/* 30 vars */]) = 0
- ... = 0
- open("/lib64/libz.so.1", O_RDONLY) = 3
- ...
- close(3) = 0
- open("/lib64/librt.so.1", O_RDONLY) = 3
- ...
- close(3) = 0
- open("/usr/lib64/libstdc++.so.6", O_RDONLY) = 3
- ...
- close(3) = 0
- open("/lib64/libm.so.6", O_RDONLY) = 3
- ...
- close(3) = 0
- open("/lib64/libgcc_s.so.1", O_RDONLY) = 3
- ...
- close(3) = 0
- open("/lib64/libc.so.6", O_RDONLY) = 3
- ...
- close(3) = 0
- open("/lib64/libpthread.so.0", O_RDONLY) = 3
- ...
- close(3) = 0
- ...
- # ldd -u demo
- Unused direct dependencies:
- /lib64/libz.so.1
- /lib64/librt.so.1
- /lib64/libm.so.6
- /lib64/libgcc_s.so.1
- # g++ -Wl,--as-needed -o demo -lz -lm -lrt main.cpp
- # ldd demo
- linux-vdso.so.1 => (0x00007fffebfff000)
- libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x00007ff665c05000)
- libc.so.6 => /lib64/libc.so.6 (0x00000036c1200000)
- libm.so.6 => /lib64/libm.so.6 (0x00000036c1e00000)
- /lib64/ld-linux-x86-64.so.2 (0x00000036c0e00000)
- libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00000036c7e00000)
- # ldd -u demo
- Unused direct dependencies:
呵呵,办法很简单省事吧,本文主要讲so依赖的一些问题,下一篇将介绍so的路径方面一些不为人知的小秘密
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。