# Dealing with Fortran include files

**URL:** https://discourse.cmake.org/t/dealing-with-fortran-include-files/11777
**Category:** Code
**Created:** [September 26, 2024, 7:37am UTC](https://discourse.cmake.org/t/dealing-with-fortran-include-files/11777 "2024-09-26T07:37:56Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ivan-pi](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ivan-pi/32/3926_2.png) [@ivan-pi](https://discourse.cmake.org/u/ivan-pi)
#### Post date: [September 26, 2024, 7:37am UTC](https://discourse.cmake.org/t/dealing-with-fortran-include-files/11777/1 "2024-09-26T07:37:57Z")

</div>

I am building a Fortran project that contains numerous include files (extension `.inc`). These are not public include files, but rather implementation details which were generated by a template mechanism.

Where do these files belong?

Currently I am using this:

```cmake
add_library(foo 
    src/foo.f90 
    src/foo_impl.inc
)

```

But I think alternatives such as:

```auto
# Alternative 1
add_library(foo
   src/foo.f90
)
target_include_directories(foo PRIVATE src/)

```

or,

```auto
# Alternative 2
add_library(foo src/foo.f90)
target_sources(foo PRIVATE foo_impl.inc)

```

would also work.

What are the differences? Which option would be best, if the generation of the `*.inc` were to be included in the CMake build?

---

<div class="post-metadata">

### Author: ![scivision](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@scivision](https://discourse.cmake.org/u/scivision)
#### Post date: [September 26, 2024, 1:04pm UTC](https://discourse.cmake.org/t/dealing-with-fortran-include-files/11777/2 "2024-09-26T13:04:22Z")

</div>

I would put CMake-generated .inc under PROJECT\_BINARY\_DIR and target\_include\_directories() that subdirectory.

---

<div class="post-metadata">

### Author: ![Lecris](https://discourse.cmake.org/user_avatar/discourse.cmake.org/lecris/32/3193_2.png) [@Lecris](https://discourse.cmake.org/u/Lecris)
#### Post date: [October 18, 2024, 6:59am UTC](https://discourse.cmake.org/t/dealing-with-fortran-include-files/11777/3 "2024-10-18T06:59:52Z")

</div>

The proper approach is the 2nd one with `target_include_directories ` (assuming you are consuming them via `#include`). If you are doing something more fancy like having those files be independently compilable Fortran submodules, then you need one of the other 2, but also you would need to change the extension to something Fortran recognizable.

---

<div class="post-metadata">

### Author: ![ivan-pi](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ivan-pi/32/3926_2.png) [@ivan-pi](https://discourse.cmake.org/u/ivan-pi)
#### Post date: [October 20, 2024, 8:48pm UTC](https://discourse.cmake.org/t/dealing-with-fortran-include-files/11777/4 "2024-10-20T20:48:28Z")

</div>

I realized that adding the `.inc` files under `add_library()` did not provide proper rebuilds when the include files were changed. Rebuilds started working when I used `target_include_directories` as both you and @scivision have recommended.

I am using an `include` line (which is technically not a Fortran statement) as a poor man’s template mechanism, but is distinct from the C #include mechanism.
