[CMake] Create executable with no dynamic dependency at all

Jean-Pierre Bergamin james at junisphere.net
Thu May 7 02:59:12 EDT 2009


Hello CMake users

We are building a CGI for a chrooted web environment. The resulting binary
must not use any dynamic libs, but link everything statically (also libc and
libstdc++ etc.).

We currently build this binary with:
  LDFLAGS = -static -lboost_regex -lz ../libs/bla.a

And the resulting gcc command line is:
  g++ -o ui foo.o -L/usr/local/lib -static -lboost_regex -lz ../libs/bla.a

The resulting binary has no dynamic dependencies at all (which is good):
# ldd ui
ldd: ui: not a dynamic ELF executable

First shot with CMake 2.6.3:
  set(Boost_USE_STATIC_LIBS ON)
  find_package(Boost 1.34.1 COMPONENTS regex)
  target_link_libraries(ui bla-static libz.a ${Boost_LIBRARIES} libthr.a)

The resulting gcc command line is:
  /usr/bin/c++     -fPIC CMakeFiles/ui.dir/foo.cpp.o -o ui  -L/usr/local/lib
libbla-static.a ../libs/bla.a -Wl,-Bstatic -lz -Wl,-Bdynamic
/usr/local/lib/libboost_regex.a -Wl,-Bstatic -lthr -Wl,-Bdynamic
-Wl,-rpath,/usr/local/lib

The first error is that the static boost lib is linked with the dynamic
flag, but anyway.

Next try:
  target_link_libraries(ui bla-static libz.a libboost_regex.a libthr.a)

Resulting gcc line:
  /usr/bin/c++     -fPIC CMakeFiles/ui.dir/foo.cpp.o -o ui -L/usr/local/lib
libbla-static.a ../libs/bla.a -Wl,-Bstatic -lz -lboost_regex -Wl,-Bdynamic
-Wl,-rpath,/usr/local/lib

The boost lib is now linked statically, but the flag "-Wl,-Bdynamic" at the
end still creates a dynamic linked executable:
# ldd ui:
        libstdc++.so.5 => /usr/lib/libstdc++.so.5 (0x2861a000)
        libm.so.4 => /lib/libm.so.4 (0x286e5000)
        libc.so.6 => /lib/libc.so.6 (0x286fb000)


So my question is how to avoid any dynamic linking at all.
I found this bug report http://www.vtk.org/Bug/view.php?id=1644 that says
that this has been implemented - but how?


Thanks for your help.

James



More information about the CMake mailing list