# Segmentation fault when running static linked executable on Ubuntu

**URL:** <https://discourse.itk.org/t/segmentation-fault-when-running-static-linked-executable-on-ubuntu/4826>\
**Category:** Engineering\
**Tags:** itk, build, cmake\
**Created:** [February 22, 2022, 9:39pm UTC](https://discourse.itk.org/t/segmentation-fault-when-running-static-linked-executable-on-ubuntu/4826 "2022-02-22T21:39:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Keyn34](https://discourse.itk.org/letter_avatar_proxy/v4/letter/k/9fc29f/32.png) [@Keyn34](https://discourse.itk.org/u/Keyn34)\
**Post date:** [February 22, 2022, 9:39pm UTC](https://discourse.itk.org/t/segmentation-fault-when-running-static-linked-executable-on-ubuntu/4826/1 "2022-02-22T21:39:59Z")

</div>

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:

```auto
[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:

```auto
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!

---

<div class="post-metadata">

**Author:** ![br\_cpvc](https://discourse.itk.org/user_avatar/discourse.itk.org/br_cpvc/32/3078_2.png) [@br\_cpvc](https://discourse.itk.org/u/br_cpvc)\
**Post date:** [November 29, 2022, 2:37pm UTC](https://discourse.itk.org/t/segmentation-fault-when-running-static-linked-executable-on-ubuntu/4826/2 "2022-11-29T14:37:26Z")

</div>

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](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.
