Dealing with Fortran include files

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:

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

But I think alternatives such as:

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

or,

# 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?

I would put CMake-generated .inc under PROJECT_BINARY_DIR and target_include_directories() that subdirectory.

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.

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.