[CMake] Problems building statically linked executable

Marcel Loose loose at astron.nl
Mon May 11 04:26:31 EDT 2009


Hi all,

I'm really struggling to build statically linked executables with CMake
(also see my previous post with subject: Static linking and
find_library).

So far, I've been able to persuade find_library() into only finding
static libraries (this is on Linux). SET(CMAKE_FIND_LIBRARY_SUFFIXES .a)
does the trick.

Furthermore I added the '-static' flag to the linker with
SET(CMAKE_EXE_LINKER_FLAGS -static).

But I still get linking errors. Here's an example of the link line
produced by CMake.

/usr/bin/g++   -W -Wall -Woverloaded-virtual -Wno-unknown-pragmas -g -O2
-fPIC -static CMakeFiles/testLogger.dir/testLogger.cc.o  -o testLogger
-rdynamic ../src/libcommon.a /opt/lofar/external/log4cplus/lib64/liblog4cplus.a -Wl,-Bstatic -lpthread -Wl,-Bdynamic

Now the biggest problem is the trailing -Wl,-Bdynamic which causes all
kinds of unresolved symbols. However, IMHO, when creating a statically
linked library any -rdynamic or -Bdynamic flags should NOT be present.
(BTW: why is liblog4cplus.a specified with full path, whereas
libpthread.a (in /usr/lib64) is simply specified as -lpthread?)

Manually invoking the command

/usr/bin/g++ -W -Wall -Woverloaded-virtual -Wno-unknown-pragmas -g -O2
-static CMakeFiles/testLogger.dir/testLogger.cc.o -o
testLogger ../src/libcommon.a /opt/lofar/external/log4cplus/lib64/liblog4cplus.a -lpthread

produces a statically linked executable; no unresolved symbol errors!

I would really love to see support being added to CMake for building
statically linked executables. For example, the add_executable() command
could be augmented with the option STATIC to indicate that the target
should be linked statically. (Maybe we could then also get rid off the
-fPIC flag, which is not needed when linking statically).

In the mean time, is there a way to get rid of these spurious
-Wl,-Bdynamic and -Wl,-Bstatic flags?

Best regards,
Marcel Loose.



More information about the CMake mailing list