static linkage: HALMD_USE_STATIC_LIBS and CMAKE_EXE_LINKER_FLAGS
The CMake variable HALMD_USE_STATIC_LIBS is cached, but changing it for a later CMake run to "ON" has not the desired effect. It is expected that HALMD_USE_STATIC_LIBS=TRUE results in a binary without any shared library dependencies (except for libcudart).
If HALMD_USE_STATIC_LIBS is toggled then prerequisites (Boost, HDF5, Lua) are searched again for static versions, but CMAKE_EXE_LINKER_FLAGS has "-static-libstdc++ -static-libgcc" only if static linking was enabled at the
first run of CMake. However, toggling HALMD_USE_STATIC_LIBS and removing CMAKE_EXE_LINKER_FLAGS (in ccmake) has an effect on linkage.
The additional linker flags are inherited from CMAKE_EXE_LINKER_FLAGS_INIT, set in cmake/platform.cmake. However, the GCC manual suggests that only -static-libstc++ is to be used (g++ may even ignore -static-libgcc):
If a library or main executable is supposed to throw or catch exceptions, you must link it [...] with the shared libgcc.
#1 Updated by Nicolas Höft about 3 years ago
One relevant bug report is here:
So, for Boost one has to add these lines:
should be similar in HDF5 and Lua
#2 Updated by Felix Höfling about 3 years ago
The Boost library, etc., are searched after the STATIC_LIBS switch is
evaluated, which sets Boost_USE_STATIC_LIBS. According to my tests, the
switch works here: toggling the flag in ccmake and re-generating flips
between static and dynamic libraries.
The example in the supposed CMake bug is a bit academic as it contains
multiple calls to find_package.