應(yīng)用場(chǎng)景
通常是你希望你的程序使用固定的C++庫版本恍箭,而不希望與他人共享程序(比如:動(dòng)態(tài)庫加載)痊项。使用了他人的特殊C++庫版本可能會(huì)導(dǎo)致意外Crash
編譯命令
g++ -static-libstdc++ main.cpp -o main
ldd 查看main涧狮,已經(jīng)未鏈接libstdc++.so.6
linux-vdso.so.1 => (0x00007ffd367e2000)
/$LIB/libonion.so => /lib64/libonion.so (0x00007f5a3d7e7000)
libm.so.6 => /lib64/libm.so.6 (0x00007f5a3d3ce000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f5a3d1b8000)
libc.so.6 => /lib64/libc.so.6 (0x00007f5a3cdf6000)
/lib64/ld-linux-x86-64.so.2 (0x00007f5a3d6d0000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007f5a3cbf2000)
編譯選項(xiàng)
-static-libstdc++:該編譯選項(xiàng)在gcc 4.5版本加入偏瓤,必須在4.5或以上的gcc版本上才可使用
When the g++ program is used to link a C++ program, it normally automatically links against libstdc++. If libstdc++ is available as a shared library, and the -static option is not used, then this links against the shared version of libstdc++. That is normally fine. However, it is sometimes useful to freeze the version of libstdc++ used by the program without going all the way to a fully static link. The -static-libstdc++ option directs the g++ driver to link libstdc++ statically, without necessarily linking other libraries statically.