基本上每一个linux 程序都至少会有一个动态库,查看某个程序使用了那些动态库,使用ldd命令查看
- # 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)
main.cpp 使用缺省参数编译结果 如果我链接一些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
- ...
大家知不知道linux从程序(program或对象)变成进程(process或进程),要经过哪些步骤呢,这里如果详细的说,估计要另开一篇文章。简单的说分三步:
1、fork进程,在内核创建进程相关内核项,加载进程可执行文件;
2、查找依赖的so,一一加载映射虚拟地址
3、初始化程序变量。
可以看到,第二步中dll依赖越多,进程启动越慢,并且发布程序的时候,这些链接但没有使用的so,同样要一起跟着发布,否则进程启动时候,会失败,找不到对应的so。所以我们不能像上面那样,把一些毫无意义的so链接进来,浪费资源。但是开发人员写makefile 一般有没有那么细心,图省事方便,那么有什么好的办法呢。继续看下去,下面会给你解决方法。
先使用 ldd -u demo 查看不需要链接的so,看下面,一面了然,无用的so全部暴露出来了吧
- # 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: