# Correct usage of EXPORT macros

**URL:** https://discourse.itk.org/t/correct-usage-of-export-macros/1304
**Category:** Engineering
**Created:** [September 25, 2018, 1:41pm UTC](https://discourse.itk.org/t/correct-usage-of-export-macros/1304 "2018-09-25T13:41:56Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![vpoughon](https://discourse.itk.org/user_avatar/discourse.itk.org/vpoughon/32/459_2.png) [@vpoughon](https://discourse.itk.org/u/vpoughon)
#### Post date: [September 25, 2018, 1:41pm UTC](https://discourse.itk.org/t/correct-usage-of-export-macros/1304/1 "2018-09-25T13:41:56Z")

</div>

In OTB we are looking at several things related to class EXPORT macros, inclusing [removing ITK\_EXPORT](https://gitlab.orfeo-toolbox.org/orfeotoolbox/otb/merge_requests/237), building with extern templates and using C++14’s `[[deprecated]]` attributes for classes, etc. As you may know we are a library heavily based on ITK. We also distribute binaries for Linux, Windows and Mac. However we cannot really figure out what the correct, portable and modern C++14 way of going about exporting symbols for both dynamic libraries and header only template code.

There is `ModuleName_EXPORT` generated by CMake, but also `ITK_TEMPLATE_EXPORT`. It’s all poorly documented and some of the git history is lost (links to old ITK jira). Plus compiler behavior is really unclear about when are symbols exported or not, depending on plateform, CMake wrapps all this in layers of macros, etc.

See also my other post here on [extern templates](https://discourse.itk.org/t/extern-templates-to-reduce-build-time/1105) somewhat related.

Any input is appreciated, especially links to official documentation.

thanks a lot!

---

<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: [September 25, 2018, 8:14pm UTC](https://discourse.itk.org/t/correct-usage-of-export-macros/1304/2 "2018-09-25T20:14:01Z")

</div>

Hi Victor,

The rules to follow are

- Use `ModuleName_EXPORT` for non-templated classes.
- Use `ITK_TEMPLATE_EXPORT` for templated classes.
- Use `ITK_FORWARD_EXPORT` for forward declarations.

This supports the all the combinations of

- macOS / Linux / Windows
- `BUILD_SHARED_LIBS` `ON` and `OFF`
- Explicit and implicit template instantiation
- `CMAKE_CXX_VISIBILITY_PRESET` set to `hidden` -\> `-fvisibility=hidden`
- `CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS:BOOL=ON`

`ITK_TEMPLATE_EXPORT` and `ITK_FORWARD_EXPORT` are defined here:

> <https://github.com/InsightSoftwareConsortium/ITK/blob/80a75d8d581fbaeb51fa0ad2e6101b435ced6968/Modules/Core/Common/include/itkMacro.h#L192-L217>

These are required for certain situations on macOS.

`ITK_EXPORT` never actually defined anything, and it can be safely removed.

HTH,  
Matt

---

<div class="post-metadata">

### Author: ![vpoughon](https://discourse.itk.org/user_avatar/discourse.itk.org/vpoughon/32/459_2.png) [@vpoughon](https://discourse.itk.org/u/vpoughon)
#### Post date: [September 27, 2018, 7:08am UTC](https://discourse.itk.org/t/correct-usage-of-export-macros/1304/3 "2018-09-27T07:08:09Z")

</div>

Thank you Matt!

---

<div class="post-metadata">

### Author: ![jhlegarreta](https://discourse.itk.org/user_avatar/discourse.itk.org/jhlegarreta/32/476_2.png) [@jhlegarreta](https://discourse.itk.org/u/jhlegarreta)
#### Post date: [October 2, 2018, 3:09am UTC](https://discourse.itk.org/t/correct-usage-of-export-macros/1304/4 "2018-10-02T03:09:12Z")

</div>

Since the `itkMacro.h` files does not generate any documentation, I’ve added this here: [https://github.com/InsightSoftwareConsortium/ITKSoftwareGuide/pull/97](https://github.com/InsightSoftwareConsortium/ITKSoftwareGuide/pull/97)

If we should rathat put it in `itkMacro.h` for ease of maintenance, let me know.

---

<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: [October 2, 2018, 8:09pm UTC](https://discourse.itk.org/t/correct-usage-of-export-macros/1304/5 "2018-10-02T20:09:28Z")

</div>

Thanks for making our ITK Software Guide a comprehensive guide @jhlegarreta!
