[CMake] Static Linking C++ Library

Brad King brad.king at kitware.com
Tue Sep 19 13:53:34 EDT 2006


Mike Melanson wrote:
> I am working on a fairly large software project that I autotool'd some
> time ago. The build system has been working reasonably well. However, I
> have hit a possible limitation that I don't know how to solve with
> autotools. So I wanted to know if CMake can solve this problem:
> 
> I need to statically link a particular libstdc++ library to the final
> build. Can CMake handle this, preferably in an elegant manner?

This is not really a problem that a build system can solve directly.
The native build tools need to be configured to support it.  In order to
link statically to the C++ runtime but dynamically to the C runtime and
other libraries you need to configure the host system properly.

For example, in order to use GCC to link statically its own libstdc++
there are two approaches:

1.) Build GCC itself with the --disable-shared option to avoid building
the shared libstdc++ in the first place.

2.) Convince the linker to choose the static libstdc++ by creating a
symlink to it in a directory that does not have the shared version and
then listing that directory first on the link line:

  ln -s /usr/lib/gcc/i486-linux-gnu/4.0.4/libstdc++.a /path/to/empty/dir
  g++ -L/path/to/empty/dir ...

Are you trying to link statically to the compiler's C++ library or some
alternative library?

-Brad


More information about the CMake mailing list