# Individual export files

**URL:** <https://discourse.slicer.org/t/individual-export-files/1743>\
**Category:** Development\
**Tags:** build\
**Created:** [December 30, 2017, 2:21am UTC](https://discourse.slicer.org/t/individual-export-files/1743 "2017-12-30T02:21:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ihnorton/32/9_2.png) [@ihnorton](https://discourse.slicer.org/u/ihnorton)\
**Post date:** [December 30, 2017, 2:21am UTC](https://discourse.slicer.org/t/individual-export-files/1743/1 "2017-12-30T02:21:54Z")

</div>

I was wondering about the export header files created for targets built by the `SlicerMacroBuild*` suite of cmake scripts. Is there a reason for all of these to be separate and configured?

```auto
Slicer-build$ find ./ -name *Export.h | wc -l
119

```

Some are unique or custom, but the significant majority are templated from `CMake/qSlicerExport.h.in`, or a slightly-different template from CTK for plugins.

I took a quick look through them:

```auto
$ cat `find ./ -name *Export.h | grep -v Plugin` | less

```

They all look identical – except for the actual name, of course – so they could potentially be replaced with a single `slicer_export.h` which defines `Slicer_EXPORT`.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [December 30, 2017, 3:19am UTC](https://discourse.slicer.org/t/individual-export-files/1743/2 "2017-12-30T03:19:23Z")

</div>

I think the current implementation is necessary if we want to allow building a module that depends on another module.

Example: Module A depends on module B. When you build module A, symbols of module A must be exported (using `__declspec(dllexport)`), while symbols of module B must be imported (using `__declspec(dllimport)`). If you used a single `Slicer_EXPORT` then both module symbols would be imported or both would be exported.

Do you have a specific idea how the current implementation could be simplified?

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ihnorton/32/9_2.png) [@ihnorton](https://discourse.slicer.org/u/ihnorton)\
**Post date:** [December 30, 2017, 5:36pm UTC](https://discourse.slicer.org/t/individual-export-files/1743/3 "2017-12-30T17:36:19Z")

</div>

I forgot that CMake is automatically adding `-D{LIBNAME}_EXPORTS` at compile time to do the switch. Thanks, it makes more sense.

> [@lassoan](#):
>
> Do you have a specific idea how the current implementation could be simplified?

In light of above, I agree this is nice and simple. We could probably make a single-header version with `-DLIBNAME=NAME` and some preprocessor substitution, but that just adds a different kind of magic, and this is overall a small point. So not worth the effort/churn.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [December 30, 2017, 10:12pm UTC](https://discourse.slicer.org/t/individual-export-files/1743/4 "2017-12-30T22:12:05Z")

</div>

> [@ihnorton](#):
>
> CMake is automatically adding -D{LIBNAME}\_EXPORTS at compile time

We’ve discussed this a bit more with @ihnorton.

Here is how CMake adds the compiler definition when it generates the target:

> <https://github.com/Kitware/CMake/blob/258e6f1b1e1d20bc48a5892f5d9d339269fa2704/Source/cmGeneratorTarget.cxx#L1685>

The same mechanism is implemented in Visual Studio IDE, so very similar macros are used when projects are created without CMake: “By default, the New Project template for a DLL adds PROJECTNAME\_EXPORTS to the defined preprocessor symbols for the DLL project.” ([source](https://msdn.microsoft.com/en-us/library/ms235636.aspx)).
