Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28519 Discussions

Fortran files included in .f90 files are not allowed to be added in cmake add_libray or set function

Junru_Zhang
Beginner
536 Views

Hi supporter, recently I met a very strange problem when building fortran codes by cmake in windows or linux. I do not know whether it is a problem in cmake or fortran compiler.  I have enclosed one demo codes in the enclosure.

However, after I change the add_library(demolib DEMO.f90 fun.f90) in the original CMakeLists.txt to add_library(demolib DEMO.f90). “make” run OK after removing “fun.f90”. Why “fun.f90” cannot be included in the Cmake add_library?

Besides, when I replace add_library(demolib DEMO.f90 fun.f90) to

set(LIB_SOURCES DEMO.f90 fun.f90)
add_library(demolib ${LIB_SOURCES})

same error will occur. Can someone explain the reason behind this?

0 Kudos
1 Solution
andrew_4619
Honored Contributor II
494 Views

The comments by mecej4 are  indeed correct but I didn't  understand it until I looked at  the source files!  fun.f90 is an INCLUDE FILE! 

View solution in original post

7 Replies
mecej4
Honored Contributor III
507 Views

The file fun.f90 does not contain a separately compilable program unit. In fact, it is included (using an "include" directive) towards the end of file demo.f90. The  file CMakeLists.txt should be modified in view of this.

For such a simple project you do not need Cmake. On the other hand, you should know the rules underlying the composition of a multi-file project, and you can learn those rules by building at the command line without Cmake. After doing so, you can replicate the build under Cmake.

andrew_4619
Honored Contributor II
495 Views

The comments by mecej4 are  indeed correct but I didn't  understand it until I looked at  the source files!  fun.f90 is an INCLUDE FILE! 

mecej4
Honored Contributor III
488 Views

I am not a Cmake user; perhaps, Cmake expects that include files are to be given a distinct suffix such as .fi or .inc ?

0 Kudos
jimdempseyatthecove
Honored Contributor III
458 Views

A problem with with Cmake/make using .f90 as a file type of an include file is that you cannot then make a general rule that "says" to the effect that all *.f90 files are to compiled to produce {same name}.obj files. Consider using a different file type (.fi, .h, .inc, etc...)

 

Jim Dempsey

0 Kudos
Junru_Zhang
Beginner
415 Views

@mecej4 @jimdempseyatthecove @andrew_4619 

 

Thanks for your reply. Yes, you are right. I should firstly understand the command line of Fortran Compiler. So I use the command line 

 

gfortran DEMO.f90 test.f90 -o test.exe

 

which can build successfully. I also tried the cmake command line which can build successfully too.

 

set(LIB_SOURCES DEMO.f90 fun.f90.inc)

 

After discuss with you, Now My question may be more clear.

Maybe I need to understand the differences in the storage and organization of .f90 and .f90.inc files when included. And Why Cmake allow the included .f90.inc to be included in the set function while the included .f90 file is not allowed. Do you know the difference? Where can I find some Clear Help Guide to be self guided?

 

 

 

 

 

0 Kudos
jimdempseyatthecove
Honored Contributor III
360 Views

>>And Why Cmake allow the included .f90.inc to be included in the set function while the included .f90 file is not allowed.

 

The compiler includes the include file into the set function (source) during compilation.

Whereas  Cmake constructs and execution of the command line for the compilation based on specified dependencies.

 

Your job is to write a Cmake file that lists the include file(s) as dependencies for a target (object file, lib file, mod file, or executable).

 

It may be helpful for you to look at this (https://stackoverflow.com/questions/13703647/how-to-properly-add-include-directories-with-cmake) which illustrates Cmake for C++ programs, which you can adapt for Fortran programs.

And (https://www.atomwitch.net/2022/04/02/cmake-fortran.html)

 

 

Jim Dempsey

 

 

0 Kudos
Junru_Zhang
Beginner
260 Views

Thanks for your reply. I will carefully read and study from these websites.

0 Kudos
Reply