Correct usage of EXPORT macros

In OTB we are looking at several things related to class EXPORT macros, inclusing removing ITK_EXPORT, 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 somewhat related.

Any input is appreciated, especially links to official documentation.

thanks a lot!

1 Like

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:

These are required for certain situations on macOS.

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

HTH,
Matt

1 Like

Thank you Matt!

1 Like

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

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

1 Like

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