# static compiled example exits with segfault

**URL:** https://discourse.itk.org/t/static-compiled-example-exits-with-segfault/2744
**Category:** Beginner Questions
**Tags:** cmake
**Created:** [February 19, 2020, 2:36pm UTC](https://discourse.itk.org/t/static-compiled-example-exits-with-segfault/2744 "2020-02-19T14:36:52Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Vincent](https://discourse.itk.org/letter_avatar_proxy/v4/letter/v/e9bcb4/32.png) [@Vincent](https://discourse.itk.org/u/Vincent)
#### Post date: [February 19, 2020, 2:36pm UTC](https://discourse.itk.org/t/static-compiled-example-exits-with-segfault/2744/1 "2020-02-19T14:36:52Z")

</div>

Dear members,  
as I am quite new to ITK, I hope someone can help me out with this static runtime crash.  
I am trying to statically compile an example from ITK to the statix ITK libraries.

make build dir  
`mkdir itk-static && cd itk-static`

build ITK STATIC  
`cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS=-march=native -DCMAKE_CXX_FLAGS=-fpermissive -DCMAKE_CXX_STANDARD=11 -DBUILD_EXAMPLES=OFF -DBUILD_TESTING=OFF -DBUILD_SHARED_LIBS=OFF ../lib/InsightToolkit-5.0.1/`

Compile  
`cmake --build . --parallel -j 8`

Now I build a small example from the ITK examples:

`mkdir /tmp/ReadAndPrintDICOMTags && cd /tmp/ReadAndPrintDICOMTags`

Use the staic itk build in the previous step  
`cmake -DITK_DIR=/tmp/itk-static -DCMAKE_BUILD_TYPE=Release -DCMAKE_EXE_LINKER_FLAGS="-static" /tmp/ITKExamples/src/IO/GDCM/ReadAndPrintDICOMTags/`

The executable runs fine. I give it a path to a dicom series. However, on exit, it gives a segfault.

`./ReadAndPrintDICOMTags /tmp/dicom/201`

output:  
…

0040|2400 =  
2050|0020 = IDENTITY  
Patient’s Name (0010|0010) is: ex010  
Segmentation fault

Tried ‘strace’ - all runs fine, until the last exit, does not return normally (exit 0 = SUCCESS) , but segfaults

…  
munmap(0x7fca25fdd000, 167936) = 0  
munmap(0x7fca24e3f000, 167936) = 0  
munmap(0x7fca262e8000, 167936) = 0  
munmap(0x7fca22c27000, 29032448) = 0  
munmap(0x7fca28be3000, 135168) = 0  
— SIGSEGV {si\_signo=SIGSEGV, si\_code=SEGV\_MAPERR, si\_addr=NULL} —  
+++ killed by SIGSEGV +++  
Segmentation fault

Does anyone see what I should do to make this work?

Thank you in advance,

Vincent

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [February 19, 2020, 3:13pm UTC](https://discourse.itk.org/t/static-compiled-example-exits-with-segfault/2744/2 "2020-02-19T15:13:01Z")

</div>

Hello @Vincent,

Welcome to the ITK community! 🌞

> [@Vincent](#):
>
> DCMAKE\_EXE\_LINKER\_FLAGS=“-static”

This can cause issues. If a binary that just works across many linux platforms is desired, check out [dockbuild](https://github.com/dockbuild/dockbuild).

Otherwise, `-static` is used with the executables generated, add `-DITK_DYNAMIC_LOADING:BOOL=OFF` to your ITK CMake configuration flags. But, dockbuild is preferred :-).

HTH,  
Matt

---

<div class="post-metadata">

### Author: ![Vincent](https://discourse.itk.org/letter_avatar_proxy/v4/letter/v/e9bcb4/32.png) [@Vincent](https://discourse.itk.org/u/Vincent)
#### Post date: [February 19, 2020, 6:06pm UTC](https://discourse.itk.org/t/static-compiled-example-exits-with-segfault/2744/3 "2020-02-19T18:06:58Z")

</div>

Thank you for your quick reply, Matt! I’ll give it a go and if it works, prolly should post some comment on this post: [https://blog.kitware.com/creating-static-executables-on-linux/](https://blog.kitware.com/creating-static-executables-on-linux/)

---

<div class="post-metadata">

### Author: ![Vincent](https://discourse.itk.org/letter_avatar_proxy/v4/letter/v/e9bcb4/32.png) [@Vincent](https://discourse.itk.org/u/Vincent)
#### Post date: [February 20, 2020, 11:41am UTC](https://discourse.itk.org/t/static-compiled-example-exits-with-segfault/2744/4 "2020-02-20T11:41:06Z")

</div>

Thanks @matt.mccormick, worked like a charm. Added ref to the above post.
