# Tutorial Step 10: missing header 'mathfunctions\_export.h'

**URL:** <https://discourse.cmake.org/t/tutorial-step-10-missing-header-mathfunctions-export-h/11092>\
**Category:** Code\
**Created:** [June 24, 2024, 1:53pm UTC](https://discourse.cmake.org/t/tutorial-step-10-missing-header-mathfunctions-export-h/11092 "2024-06-24T13:53:47Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![codefrog](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/22d042/32.png) [@codefrog](https://discourse.cmake.org/u/codefrog)\
**Post date:** [June 24, 2024, 1:53pm UTC](https://discourse.cmake.org/t/tutorial-step-10-missing-header-mathfunctions-export-h/11092/1 "2024-06-24T13:53:47Z")

</div>

I’m just run through Step 10 of the Tutorial, and am trying to use the GenerateExportHeader module.  
However, when I build, I get the error:

```auto
fatal error: mathfunctions_export.h: No such file or directory
   10 | #include "mathfunctions_export.h"

```

In MathFunctions/CMakeFiles.txt I’ve added:

```auto
include(GenerateExportHeader)
generate_export_header(MathFunctions)

```

I can see the generated file: ‘Step10\_build/MathFunctions/mathfunctions\_export.h’.

MathFunctions.h includes the new generated file.

What am I missing?

---

<div class="post-metadata">

**Author:** ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)\
**Post date:** [June 24, 2024, 2:00pm UTC](https://discourse.cmake.org/t/tutorial-step-10-missing-header-mathfunctions-export-h/11092/2 "2024-06-24T14:00:35Z")

</div>

Probably missing [target\_include\_directories()](https://cmake.org/cmake/help/latest/command/target_include_directories.html) or linking to the library target?  
I am not familiar with that project, and you haven’t provided what you have.

---

<div class="post-metadata">

**Author:** ![codefrog](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/22d042/32.png) [@codefrog](https://discourse.cmake.org/u/codefrog)\
**Post date:** [June 24, 2024, 2:02pm UTC](https://discourse.cmake.org/t/tutorial-step-10-missing-header-mathfunctions-export-h/11092/3 "2024-06-24T14:02:10Z")

</div>

This is the link to Step 10 of the tutorial: [Step 10](https://cmake.org/cmake/help/latest/guide/tutorial/Selecting%20Static%20or%20Shared%20Libraries.html)

MathFunctions/CMakeLists.txt includes:

```auto
target_include_directories(MathFunctions
                           INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
                           )

```

The source code is held at: [tutorial archive](https://cmake.org/cmake/help/latest/_downloads/2f714788f33bb2e6f33d6df03791dea2/cmake-3.30.0-rc3-tutorial-source.zip)

This is the contents of my `Step10_build/MathFunctions folder`:  
`CMakeFiles cmake_install.cmake libSqrtLibrary.a Makefile MakeTable mathfunctions_export.h Table.h`

**The missing line is the following**??:  
`target_include_directories(MathFunctions PUBLIC ${CMAKE_CURRENT_BINARY_DIR})`

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Post date:** [June 24, 2024, 2:58pm UTC](https://discourse.cmake.org/t/tutorial-step-10-missing-header-mathfunctions-export-h/11092/4 "2024-06-24T14:58:41Z")

</div>

This should help:

```cmake
# cmake-format: off
target_sources(
  MathFunctions PUBLIC
    FILE_SET HEADERS
    BASE_DIRS ${PROJECT_BINARY_DIR}
    FILES ${PROJECT_BINARY_DIR}/mathfunctions_export.h
)
# cmake-format: on

```

---

<div class="post-metadata">

**Author:** ![codefrog](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/22d042/32.png) [@codefrog](https://discourse.cmake.org/u/codefrog)\
**Post date:** [June 24, 2024, 3:06pm UTC](https://discourse.cmake.org/t/tutorial-step-10-missing-header-mathfunctions-export-h/11092/5 "2024-06-24T15:06:31Z")

</div>

> [@codefrog](#):
>
> target\_include\_directories(MathFunctions PUBLIC ${CMAKE\_CURRENT\_BINARY\_DIR})

Hi, @ClausKlein Is that an alternative to my solution where I set the target\_include\_directories? Or should I be using ‘target\_sources’ instead?

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Post date:** [June 24, 2024, 3:08pm UTC](https://discourse.cmake.org/t/tutorial-step-10-missing-header-mathfunctions-export-h/11092/6 "2024-06-24T15:08:52Z")

</div>

This is the modern way to set the include path and install public headers.

---

<div class="post-metadata">

**Author:** ![codefrog](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/22d042/32.png) [@codefrog](https://discourse.cmake.org/u/codefrog)\
**Post date:** [June 24, 2024, 3:22pm UTC](https://discourse.cmake.org/t/tutorial-step-10-missing-header-mathfunctions-export-h/11092/7 "2024-06-24T15:22:53Z")

</div>

@ClausKlein I’ve updated my MathFunctions/CMakeLists.txt with your change.  
Like so:

```auto
include(GenerateExportHeader)
add_library(MathFunctions MathFunctions.cxx )
generate_export_header(MathFunctions EXPORT_FILE_NAME 
                        ${PROJECT_BINARY_DIR}/exports/mathfunctions_export.h)
target_compile_definitions(MathFunctions PRIVATE "EXPORTING_MYMATH")
target_include_directories(MathFunctions
                           INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
                           )
target_sources(
  MathFunctions PUBLIC
    FILE_SET HEADERS
    BASE_DIRS ${PROJECT_BINARY_DIR}
    FILES ${PROJECT_BINARY_DIR}/exports/mathfunctions_export.h
)

```

But still get the error message:

```auto
Step10/MathFunctions/MathFunctions.h:10:10: fatal error: mathfunctions_export.h: No such file or directory
   10 | #include "mathfunctions_export.h"
      | ^ ~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.

```

Clearly I’m still missing something obvious here ☹

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Post date:** [June 24, 2024, 5:03pm UTC](https://discourse.cmake.org/t/tutorial-step-10-missing-header-mathfunctions-export-h/11092/8 "2024-06-24T17:03:11Z")

</div>

```cmake
include(GenerateExportHeader)
generate_export_header(MathFunctions EXPORT_FILE_NAME
                       ${CMAKE_CURRENT_BINARY_DIR}/exports/mathfunctions_export.h
                       )

# cmake-format: off
target_sources(
  MathFunctions PUBLIC
    FILE_SET HEADERS
    BASE_DIRS ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}
    FILES ${CMAKE_CURRENT_BINARY_DIR}/exports/mathfunctions_export.h
          ${CMAKE_CURRENT_SOURCE_DIR}/MathFunctions.h

)
# cmake-format: on

```

The include path is set to `CMAKE_CURRENT_BINARY_DIR` and `CMAKE_CURRENT_SOURCE_DIR`.

And the “`EXPORTING_MYMATH`” is not longer needed and wrong.

Do not forget to install it:

```cmake
install(TARGETS ${installable_libs} DESTINATION lib FILE_SET HEADERS)

```

---

<div class="post-metadata">

**Author:** ![codefrog](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/22d042/32.png) [@codefrog](https://discourse.cmake.org/u/codefrog)\
**Post date:** [June 24, 2024, 5:59pm UTC](https://discourse.cmake.org/t/tutorial-step-10-missing-header-mathfunctions-export-h/11092/9 "2024-06-24T17:59:09Z")

</div>

Thanks @ClausKlein, that works very well, and keeps the exported headers during build local to the library. It also installs perfectly. My final version (hopefully) is as follows:

```auto
include(GenerateExportHeader)

# add the library that runs
add_library(MathFunctions MathFunctions.cxx )

generate_export_header(MathFunctions EXPORT_FILE_NAME 
                         ${CMAKE_CURRENT_BINARY_DIR}/exports/mathfunctions_export.h)

# state that anybody linking to us needs to include the current source dir
# to find MathFunctions.h, while we don't.
target_include_directories(MathFunctions
                           INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
                           )

target_include_directories(MathFunctions PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/exports)

#BASE_DIRS ${PROJECT_BINARY_DIR}
target_sources(
  MathFunctions PUBLIC
    FILE_SET HEADERS
    BASE_DIRS ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}
    FILES ${CMAKE_CURRENT_BINARY_DIR}/exports/mathfunctions_export.h
         ${CMAKE_CURRENT_SOURCE_DIR}/MathFunctions.h
)

# link MathFunctions to tutorial_compiler_flags
target_link_libraries(MathFunctions PUBLIC tutorial_compiler_flags)

# install libs
set(installable_libs MathFunctions tutorial_compiler_flags)
if(TARGET SqrtLibrary)
  list(APPEND installable_libs SqrtLibrary)
endif()

install(TARGETS ${installable_libs} DESTINATION lib FILE_SET HEADERS)

# install include headers
install(FILES MathFunctions.h DESTINATION include)

```

However I assume there should not be any reason why I can’t change the build to use ‘USE\_MYMATH’.

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Post date:** [June 24, 2024, 6:05pm UTC](https://discourse.cmake.org/t/tutorial-step-10-missing-header-mathfunctions-export-h/11092/10 "2024-06-24T18:05:57Z")

</div>

The calls to

> [@codefrog](#):
>
> `target_include_directories()`

should not be needed.

And the last `install(FILES ...)` call is also not needed.

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Post date:** [June 25, 2024, 7:16am UTC](https://discourse.cmake.org/t/tutorial-step-10-missing-header-mathfunctions-export-h/11092/11 "2024-06-25T07:16:38Z")

</div>

@ben.boeckel should we modernize the Tutorial?

---

<div class="post-metadata">

**Author:** ![codefrog](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/22d042/32.png) [@codefrog](https://discourse.cmake.org/u/codefrog)\
**Post date:** [June 25, 2024, 11:02am UTC](https://discourse.cmake.org/t/tutorial-step-10-missing-header-mathfunctions-export-h/11092/12 "2024-06-25T11:02:41Z")

</div>

Thanks for all your help @ClausKlein !  
I still seemed to need just one `target_include_directories()`. It will not build otherwise.

Have removed the final `install(FILES ...)`  
Installs OK!

This looks a lot tidier 😀 :

```auto
include(GenerateExportHeader)

# add the library that runs
add_library(MathFunctions MathFunctions.cxx )

generate_export_header(MathFunctions EXPORT_FILE_NAME 
                         ${CMAKE_CURRENT_BINARY_DIR}/exports/mathfunctions_export.h)

target_include_directories(MathFunctions PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/exports)

target_sources(
  MathFunctions PUBLIC
    FILE_SET HEADERS
    BASE_DIRS ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}
    FILES ${CMAKE_CURRENT_BINARY_DIR}/exports/mathfunctions_export.h
         ${CMAKE_CURRENT_SOURCE_DIR}/MathFunctions.h
)

# link MathFunctions to tutorial_compiler_flags
target_link_libraries(MathFunctions PUBLIC tutorial_compiler_flags)

# install libs
set(installable_libs MathFunctions tutorial_compiler_flags)
if(TARGET SqrtLibrary)
  list(APPEND installable_libs SqrtLibrary)
endif()

install(TARGETS ${installable_libs} DESTINATION lib FILE_SET HEADERS)

```

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Post date:** [June 25, 2024, 11:31am UTC](https://discourse.cmake.org/t/tutorial-step-10-missing-header-mathfunctions-export-h/11092/13 "2024-06-25T11:31:22Z")

</div>

I would not use this exports subdirectory!

```auto
-- Install configuration: ""
-- Installing: /usr/local/lib/libMathFunctions.dylib
-- Installing: /usr/local/lib/MathFunctions.h
-- Installing: /usr/local/lib/exports/mathfunctions_export.h
-- Installing: /usr/local/lib/libSqrtLibrary.a
-- Installing: /usr/local/bin/Tutorial
-- Installing: /usr/local/include/TutorialConfig.h
bash-5.2$ 

```

If you do, you must also change your code:

```c++
#pragma once

#include <exports/mathfunctions_export.h>

namespace mathfunctions {
double MATHFUNCTIONS_EXPORT sqrt(double x);
}

```

This

```cmake
target_include_directories(MathFunctions PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/exports)

```

helps you build your code, but does not help the user trying to use your installed files!

---

<div class="post-metadata">

**Author:** ![codefrog](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/22d042/32.png) [@codefrog](https://discourse.cmake.org/u/codefrog)\
**Post date:** [June 26, 2024, 2:06pm UTC](https://discourse.cmake.org/t/tutorial-step-10-missing-header-mathfunctions-export-h/11092/14 "2024-06-26T14:06:19Z")

</div>

Thanks @ClausKlein that’s a very valid point. With just that final change I have:

```auto
include(GenerateExportHeader)

# add the library that runs
add_library(MathFunctions MathFunctions.cxx )

generate_export_header(MathFunctions EXPORT_FILE_NAME 
                         ${CMAKE_CURRENT_BINARY_DIR}/mathfunctions_export.h)

target_include_directories(MathFunctions PUBLIC ${CMAKE_CURRENT_BINARY_DIR})

target_sources(
  MathFunctions PUBLIC
    FILE_SET HEADERS
    BASE_DIRS ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}
    FILES ${CMAKE_CURRENT_BINARY_DIR}/mathfunctions_export.h
         ${CMAKE_CURRENT_SOURCE_DIR}/MathFunctions.h
)

# link MathFunctions to tutorial_compiler_flags
target_link_libraries(MathFunctions PUBLIC tutorial_compiler_flags)

# install libs
set(installable_libs MathFunctions tutorial_compiler_flags)
if(TARGET SqrtLibrary)
  list(APPEND installable_libs SqrtLibrary)
endif()

install(TARGETS ${installable_libs} DESTINATION lib FILE_SET HEADERS)

```

This builds correctly. And when I install, I get:

```auto
-- Install configuration: ""
-- Installing: /usr/local/lib/libMathFunctions.so
-- Installing: /usr/local/lib/mathfunctions_export.h
-- Up-to-date: /usr/local/lib/MathFunctions.h
-- Installing: /usr/local/bin/Tutorial
-- Set non-toolchain portion of runtime path of "/usr/local/bin/Tutorial" to ""
-- Installing: /usr/local/include/TutorialConfig.h

```

Many thanks! 😀
