linux下so动态库一些不为人知的秘密(上)
睿丰德科技 专注RFID识别技术和条码识别技术与管理软件的集成项目。质量追溯系统、MES系统、金蝶与条码系统对接、用友与条码系统对接
linux 下有动态库和静态库,动态库以.so为扩展名,静态库以.a为扩展名。二者都使用广泛。本文主要讲动态库方面知识。
基本上每一个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)
这么多so,是的。使用ldd显示的so,并不是所有so都是需要使用的,下面举个例子
main.cpp- #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的路径方面一些不为人知的小秘密
RFID管理系统集成商 RFID中间件 条码系统中间层 物联网软件集成