# Address Sanitizer / Consume ITK as Source Code

**URL:** https://discourse.itk.org/t/address-sanitizer-consume-itk-as-source-code/2614
**Category:** Engineering
**Created:** [January 14, 2020, 9:17am UTC](https://discourse.itk.org/t/address-sanitizer-consume-itk-as-source-code/2614 "2020-01-14T09:17:13Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![spinicist](https://discourse.itk.org/user_avatar/discourse.itk.org/spinicist/32/183_2.png) [@spinicist](https://discourse.itk.org/u/spinicist)
#### Post date: [January 14, 2020, 9:17am UTC](https://discourse.itk.org/t/address-sanitizer-consume-itk-as-source-code/2614/1 "2020-01-14T09:17:13Z")

</div>

Hello,

This is a question where I feel I don’t know quite enough to ask the right question, so if I’m doing it wrong feel free to enlighten me.

I recently discovered the joy of Address Sanitizer for debugging memory issues (which I sadly still make). The problem with Address Sanitizer is that _all_ your code has to be compiled with it, but it entails a significant performance hit ([https://github.com/google/sanitizers/wiki/AddressSanitizerPerformanceNumbers](https://github.com/google/sanitizers/wiki/AddressSanitizerPerformanceNumbers)) which means you don’t want to leave it on all the time. If you don’t compile and link all code with Address Sanitizer, then it will hit false positives almost immediately upon program start (often in `std::`!).

My approach in projects where everything is compiled from source is to add the following in `CMakeLists`:

```auto
set(USE_ASAN $<$<CONFIG:RelWithDebInfo>:-fsanitize=address>)
target_compile_options(Project PRIVATE ${USE_ASAN})
target_link_options(Project PRIVATE ${USE_ASAN})

```

I then mostly develop in `Release` mode, and if I hit a problem I recompile in `RelWithDebInfo`.

This doesn’t work well in projects with an externally compiled library - e.g. ITK. If I want to take this approach, I either have to remember to go off and rebuild ITK with Address Sanitizer, or I need to keep two copies of ITK compiled, one with Address Sanitizer and one without, and swap which one I’m linking to in my project based on the build type.

I think I could make the second approach work, but then I started wondering whether it is actually possible to consume ITK purely as source-code in my project? I.e. don’t build ITK as a separate library then link it to my project, but build it all within my project as a single step? I am aware that this may not be optimal either, as every time I switch between `Release` and `RelWithDebInfo` it will require rebuilding a lot of files. But given ITK is a lot of template header code anyway, this may not be so bad.

Does anyone have any opinions on how best to approach this? Thanks in advance.

---

<div class="post-metadata">

### Author: ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)
#### Post date: [January 14, 2020, 1:47pm UTC](https://discourse.itk.org/t/address-sanitizer-consume-itk-as-source-code/2614/2 "2020-01-14T13:47:03Z")

</div>

Hello @spinicist,

I suspect this is a use case for a [CMake Superbuild](https://blog.kitware.com/cmake-superbuilds-git-submodules/) so that your itk is configured in the same way as your general project, passing the flags to the ITK build.

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [January 14, 2020, 2:07pm UTC](https://discourse.itk.org/t/address-sanitizer-consume-itk-as-source-code/2614/3 "2020-01-14T14:07:46Z")

</div>

It sounds like you want a super-build. Some ready-to-steal examples of a super-build are [PlusBuild](https://github.com/PlusToolkit/PlusBuild) and [Slicer](https://github.com/Slicer/Slicer/blob/master/CMakeLists.txt).

---

<div class="post-metadata">

### Author: ![spinicist](https://discourse.itk.org/user_avatar/discourse.itk.org/spinicist/32/183_2.png) [@spinicist](https://discourse.itk.org/u/spinicist)
#### Post date: [January 14, 2020, 3:11pm UTC](https://discourse.itk.org/t/address-sanitizer-consume-itk-as-source-code/2614/4 "2020-01-14T15:11:35Z")

</div>

Thanks both.

Super-Builds are an odd one. I don’t think they are widely used outside of the ITK sphere? ANTs is the only project I’ve actually encountered one on before now. I’ve been put off them because they seem like yet another layer of complication for a C++ build. I guess the root cause of this is the lack of proper package management system in C++?

I found a minimal example which looks easier to work with than PlusBuild or Slicer: [https://github.com/Sarcasm/cmake-superbuild](https://github.com/Sarcasm/cmake-superbuild). However I’m also tempted to look into conan or another package manager as well, if I find the time.

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [January 14, 2020, 3:36pm UTC](https://discourse.itk.org/t/address-sanitizer-consume-itk-as-source-code/2614/5 "2020-01-14T15:36:49Z")

</div>

If another example will help, SimpleITK also uses a Superbuild to build ITK:

> **[SimpleITK/SimpleITK](https://github.com/SimpleITK/SimpleITK/tree/master/SuperBuild)**
>
> SimpleITK: a layer built on top of the Insight Toolkit (ITK), intended to simplify and facilitate ITK's use in rapid prototyping, education and interpreted languages. - SimpleITK/SimpleITK

It’s a little different in that the top level CMakeLists.txt is a normal project. But if you configure with the “Superbuild” directory, then it build ITK and SWIG.

---

<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: [January 14, 2020, 4:09pm UTC](https://discourse.itk.org/t/address-sanitizer-consume-itk-as-source-code/2614/6 "2020-01-14T16:09:00Z")

</div>

In my experience, the most efficient approach for this use case is to keep two build trees around and switch as needed.

---

<div class="post-metadata">

### Author: ![spinicist](https://discourse.itk.org/user_avatar/discourse.itk.org/spinicist/32/183_2.png) [@spinicist](https://discourse.itk.org/u/spinicist)
#### Post date: [January 14, 2020, 4:15pm UTC](https://discourse.itk.org/t/address-sanitizer-consume-itk-as-source-code/2614/7 "2020-01-14T16:15:26Z")

</div>

@matt.mccormick In terms of minimising compile-time, I think you are right. The question is whether there is a way to write my `CMakeLists.txt` (in my project) to pick the ITK directory depending on the build type, rather than having to change a variable manually. Otherwise I’ll forget to change it!

---

<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: [January 14, 2020, 4:29pm UTC](https://discourse.itk.org/t/address-sanitizer-consume-itk-as-source-code/2614/8 "2020-01-14T16:29:15Z")

</div>

@spinicist yes. In practice, it is nice to have dedicated build configurations, including for the project, e.g.

```auto
ITK-Release-build/
MyProject-Release-build/

ITK-RelWithDebInfo-build/
MyProject-RelWithDebInfo-build/

```

Then, switch to the dedicated project `MyProject-RelWithDebInfo-build` tree and re-build, when ASan checks are desired.

---

<div class="post-metadata">

### Author: ![spinicist](https://discourse.itk.org/user_avatar/discourse.itk.org/spinicist/32/183_2.png) [@spinicist](https://discourse.itk.org/u/spinicist)
#### Post date: [January 14, 2020, 4:32pm UTC](https://discourse.itk.org/t/address-sanitizer-consume-itk-as-source-code/2614/9 "2020-01-14T16:32:46Z")

</div>

That’s an interesting idea. I don’t think it would play well with the VSCode CMake plug-in (which assumes a single build directory). I’ll have a try though…

---

<div class="post-metadata">

### Author: ![imikejackson](https://discourse.itk.org/user_avatar/discourse.itk.org/imikejackson/32/35_2.png) [@imikejackson](https://discourse.itk.org/u/imikejackson)
#### Post date: [January 16, 2020, 1:48pm UTC](https://discourse.itk.org/t/address-sanitizer-consume-itk-as-source-code/2614/10 "2020-01-16T13:48:48Z")

</div>

We set the ITK\_DIR based on the CMAKE\_BUILD\_TYPE variable and seems to work with both VS and ninja builds.
