[CMake] Static Linking C++ Library
Brad King
brad.king at kitware.com
Tue Sep 19 14:32:15 EDT 2006
Mike Melanson wrote:
> Brad King wrote:
>> You can link the entire program statically if you don't have any of your
>> own shared libraries. Then running "ldd myexe" will produce a message
>> that the executable is not dynamically linked. This requires just
>> adding "-static" to CMAKE_EXE_LINKER_FLAGS (or the appropriate option
>> for your compiler).
>
> Sounds simple enough. Except that there are 2 build targets for this
> project that embody most of the same functionality: A static executable
> and a shared library that other programs can call. That shared library
> changes the rules a bit, doesn't it?
Yes, specially if the shared library uses the C++ runtime. In that case
you cannot use the static C++ runtime at all!
If you are trying to create a binary tarball distribution then you will
have to include the C++ library in it (and then put the source to the
GNU libstdc++ in a link off your download page to satisfy the license).
We have several projects that do this. They key to distributing
dynamically linked binaries in a tarball is to use a "shared forwarding"
executable to setup LD_LIBRARY_PATH correctly and then invoke the real
executable. There is actually code that comes in the source of CMake
you can use for this:
http://www.cmake.org/cgi-bin/viewcvs.cgi/Source/kwsys/SharedForward.h.in?root=CMake&view=markup
The forwarding executable is a small C program that does not link to
anything that isn't provided everywhere. It knows the relative path to
the shared libraries and the executable. It sets up LD_LIBRARY_PATH in
its own environment and then runs the real executable with "execv".
This allows the application to run from anywhere that its tarball is
extracted.
-Brad
More information about the CMake
mailing list