[CMake] Creating static executables

Alan W. Irwin irwin at beluga.phys.uvic.ca
Fri Jun 8 15:08:31 EDT 2007


On 2007-06-08 14:37-0400 Ajay Divekar wrote:

> I am trying to create a static executable and for that I have already defined
> using ADD_Definitions the -static option.
>
> But after it is build and when I do
> ldd bbb (where bbb is the executable that was just generated)
>
> 	The output of the above command is
>
> libz.so.3 => /lib/libz.so.3 (0x281df000)
> libstdc++.so.6=> /usr/local/lib/gcc/i386-portbld-freebsd6.2/3.4.6/libstdc++.so.6
> libm.so.4 => /lib/libm.so.4 (0x282b1000)
> libc.so.6 => /lib/libc.so.6 (0x282c7000)
> libgcc_s.so.1=> /usr/local/lib/gcc/i386-portbld-freebsd6.2/3.4.6/libgcc_s.so.1
> libz.so.2 => /lib/libz.so.2 (0x2814f000)
> libm.so.3 => /lib/libm.so.3 (0x2815f000)
> libc.so.5 => /lib/libc.so.5 (0x2817a000)
>
> I want my executable to be static as this will make it completely platform
> independent.
>
> All these are system libraries and some of them are gcc releated. Is there
> anyway to create a static library library without any shared library
> dependency.

Let me make a general comment first. Virtually every platform has a linker
option for building static executables.  For example, on Linux see the
-Bstatic (or -static) linker option documented in 'man ld'.  Once you have
decided what linker option you need for static executables on your
platform(s), then you have to tell cmake about it.

Now to get specific, I don't think you specified the -static linker option
correctly above when you specified -static with ADD_DEFINITIONS.  Use
-DCMAKE_VERBOSE_MAKEFILE=ON to see exactly where that -static option is
being used.  My guess is it would just be the compile stage and not the link
stage.  I suggest you try the LINK_FLAGS property of SET_TARGET_PROPERTIES,
instead.

Alan
__________________________
Alan W. Irwin

Astronomical research affiliation with Department of Physics and Astronomy,
University of Victoria (astrowww.phys.uvic.ca).

Programming affiliations with the FreeEOS equation-of-state implementation
for stellar interiors (freeeos.sf.net); PLplot scientific plotting software
package (plplot.org); the libLASi project (unifont.org/lasi); the Loads of
Linux Links project (loll.sf.net); and the Linux Brochure Project
(lbproject.sf.net).
__________________________

Linux-powered Science
__________________________


More information about the CMake mailing list