Segmentation fault when running static linked executable on Ubuntu

Hey everyone (again :)),

I’m trying to run a static linked executable on Ubuntu 20.04.3. For this purpose, I configured ITK 5.2.1 with CMake, made sure to set BUILD_SHARED_LIBS and ITK_DYNAMIC_LOADING to OFF, and proceeded normally with make and make install. The build and install process runs successfully and ITK and its static libraries are written to the CMAKE_INSTALL_PREFIX path.

When I now try to build the application with VisualStudioCode everything works well, but I get (to my knowledge) two very important warnings:

[build] /usr/bin/ld: /home/keyn34/libs/ITK/install-static/lib/libitkhdf5-static-5.2.a(H5PLint.c.o): in function `itk_H5PL__open':
[build] H5PLint.c:(.text+0x533): warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
[build] /usr/bin/ld: /home/keyn34/libs/ITK/install-static/lib/libitksys-5.2.a(SystemTools.cxx.o): in function `itksys::SystemTools::ConvertToUnixSlashes(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&)':
[build] SystemTools.cxx:(.text+0x1de4): warning: Using 'getpwnam' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking

After that, the executable can be used normally, but the moment a file is read with the ITK FileReader it immediately crashes with a Segmentation fault (core dumped) error.
It was perfectly possible to build and execute the application when it was dynamically linked against ITK on Ubuntu and Windows, so I assume the issue only persists in static link mode.

Here is also the CMake file content for the project:

cmake_minimum_required (VERSION 3.16.2)
set (CMAKE_CXX_STANDARD 17)

project ("exe_static")

set(CMAKE_PREFIX_PATH "/home/keyn34/libs/ITK/install-static/")

set(ITK_DIR "/home/keyn34/libs/ITK/install-static/")

find_package(ITK REQUIRED)
include(${ITK_USE_FILE})

set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
set(BUILD_SHARED_LIBS OFF)
set(CMAKE_EXE_LINKER_FLAGS "-static")

# Include sub-projects.
add_subdirectory ("exe_static")

target_link_libraries(exe_static${ITK_LIBRARIES})

Any suggestion what I am doing wrong here?
Thank you in advance!

I have had simular problems with static linking the ANTs project against ITK on different versions of Ubuntu.

For me the problem seem to be the gcc/g++ compile version used.

I think my problems stems from the weak point bug discussed in:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95989

Where the solution is to use a compiler version higher than 10.3.

I found that using gcc/g++ version 11 from the Ubuntu 22.04 solves the problems I have been having.

2 Likes