[CMake] Create executable with no dynamic dependency at all

Jean-Pierre Bergamin james at junisphere.net
Thu May 7 04:45:00 EDT 2009


Hello

> > 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?

> I am not sure, how you get -Wl,-Bstatic and so on. Here is how I get
> static executable:
> 
> $ /usr/bin/c++  -O2 -fomit-frame-pointer -fPIC -s -static \
> CMakeFiles/DispatcherExe.dir/DispatcherMain.cc.o \
> -o DispatcherExe \
> -rdynamic ../../Generic/libGeneric.a \
> ../../CorbaUtil/libCorbaUtil.a -lmico2.3.13 -lmicocoss2.3.13 -lssl -
> lcrypto -lpthread -ldl
> 
> $ ldd DispatcherExe
>         not a dynamic executable
> 
> Here is how I link it:
> 
> ADD_LIBRARY(Generic STATIC ${GENERIC_SRCS})
> 
> ADD_LIBRARY(CorbaUtil STATIC ${CORBAUTIL_SRCS})
> TARGET_LINK_LIBRARIES(CorbaUtil  ${MICO_LIBRARY} ${MICO_COSS_LIBRARY}
> ssl crypto pthread dl)
> 
> ADD_EXECUTABLE(DispatcherExe  ${OPERDISPATCHER_SRCS})
> TARGET_LINK_LIBRARIES(DispatcherExe  CorbaUtil Generic)


May I ask what platform you are using? I just tried the following simplest
example on FreeBSD 6 and on Ubuntu 8.10. On both platforms I do not manage
to get a non-dyanamic executable. How do I get the "-static" flag set?


Regards

James


$ cat CMakeLists.txt 
add_library(mylib STATIC mylib.cpp)
add_executable(static static.cpp)
target_link_libraries(static mylib)

$ cat mylib.h
void f();

$ cat mylib.cpp 
#include <iostream>
#include "mylib.h"

void f() {
        std::cout << "mylib speaking..." << std::endl;
}

$ cat static.cpp 
#include "mylib.h"

int main() {
        f();
        return 0;
}

$ cmake .
-- The C compiler identification is GNU
-- The CXX compiler identification is GNU
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/james/test/cmake

$ make VERBOSE=1
/usr/local/bin/cmake -H/home/james/test/cmake -B/home/james/test/cmake
--check-build-system CMakeFiles/Makefile.cmake 0
/usr/local/bin/cmake -E cmake_progress_start
/home/james/test/cmake/CMakeFiles
/home/james/test/cmake/CMakeFiles/progress.make
make -f CMakeFiles/Makefile2 all
make -f CMakeFiles/mylib.dir/build.make CMakeFiles/mylib.dir/depend
cd /home/james/test/cmake && /usr/local/bin/cmake -E cmake_depends "Unix
Makefiles" /home/james/test/cmake /home/james/test/cmake
/home/james/test/cmake /home/james/test/cmake
/home/james/test/cmake/CMakeFiles/mylib.dir/DependInfo.cmake --color=
Dependee "/home/james/test/cmake/CMakeFiles/mylib.dir/DependInfo.cmake" is
newer than depender
"/home/james/test/cmake/CMakeFiles/mylib.dir/depend.internal".
Scanning dependencies of target mylib
make -f CMakeFiles/mylib.dir/build.make CMakeFiles/mylib.dir/build
/usr/local/bin/cmake -E cmake_progress_report
/home/james/test/cmake/CMakeFiles 1
[ 50%] Building CXX object CMakeFiles/mylib.dir/mylib.cpp.o
/usr/bin/c++     -o CMakeFiles/mylib.dir/mylib.cpp.o -c
/home/james/test/cmake/mylib.cpp
Linking CXX static library libmylib.a
/usr/local/bin/cmake -P CMakeFiles/mylib.dir/cmake_clean_target.cmake
/usr/local/bin/cmake -E cmake_link_script CMakeFiles/mylib.dir/link.txt
--verbose=1
/usr/bin/ar cr libmylib.a  CMakeFiles/mylib.dir/mylib.cpp.o
/usr/bin/ranlib libmylib.a
/usr/local/bin/cmake -E cmake_progress_report
/home/james/test/cmake/CMakeFiles  1
[ 50%] Built target mylib
make -f CMakeFiles/static.dir/build.make CMakeFiles/static.dir/depend
cd /home/james/test/cmake && /usr/local/bin/cmake -E cmake_depends "Unix
Makefiles" /home/james/test/cmake /home/james/test/cmake
/home/james/test/cmake /home/james/test/cmake
/home/james/test/cmake/CMakeFiles/static.dir/DependInfo.cmake --color=
Dependee "/home/james/test/cmake/CMakeFiles/static.dir/DependInfo.cmake" is
newer than depender
"/home/james/test/cmake/CMakeFiles/static.dir/depend.internal".
Scanning dependencies of target static
make -f CMakeFiles/static.dir/build.make CMakeFiles/static.dir/build
/usr/local/bin/cmake -E cmake_progress_report
/home/james/test/cmake/CMakeFiles 2
[100%] Building CXX object CMakeFiles/static.dir/static.cpp.o
/usr/bin/c++     -o CMakeFiles/static.dir/static.cpp.o -c
/home/james/test/cmake/static.cpp
Linking CXX executable static
/usr/local/bin/cmake -E cmake_link_script CMakeFiles/static.dir/link.txt
--verbose=1
/usr/bin/c++     -fPIC CMakeFiles/static.dir/static.cpp.o  -o static
libmylib.a 
/usr/local/bin/cmake -E cmake_progress_report
/home/james/test/cmake/CMakeFiles  2
[100%] Built target static
/usr/local/bin/cmake -E cmake_progress_start
/home/james/test/cmake/CMakeFiles 0

$ ldd static
static:
        libstdc++.so.5 => /usr/lib/libstdc++.so.5 (0x2807a000)
        libm.so.4 => /lib/libm.so.4 (0x28145000)
        libc.so.6 => /lib/libc.so.6 (0x2815b000)

$ ./static 
mylib speaking...



More information about the CMake mailing list