linking libstdc++ statically in CMake
Friday, December 7th, 2007For making portable binaries of linux programs, it helps to link libstdc++ statically, since it varies a lot from system to system.
If you are using CMake, this takes a little shuffling. Take a copy of the static libstdc++*.a library (on Debian, this was in /usr/lib/gcc/…, place it in a subdirectory (say “additions/libstdc”), and then add:
LINK_DIRECTORIES(additions/libstdc)
ADD_DEFINITIONS(-static-libgcc)
SET(CMAKE_CXX_LINK_EXECUTABLE “${CMAKE_CXX_LINK_EXECUTABLE} -static-libgcc”)
That should do it. You can tell whether it worked by doing “ldd your-program-name” and making sure libstdc++ doesn’t show up there anymore.
There are subtleties you should be aware of, see the discussion at trilithium.
