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

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

Junru_Zhang
初学者
2,632 次查看

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 项奖励
1 解答
andrew_4619
名誉分销商 III
2,590 次查看

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! 

在原帖中查看解决方案

7 回复数
mecej4
名誉分销商 III
2,603 次查看

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
名誉分销商 III
2,591 次查看

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
名誉分销商 III
2,584 次查看

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 项奖励
jimdempseyatthecove
名誉分销商 III
2,554 次查看

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 项奖励
Junru_Zhang
初学者
2,511 次查看

@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 项奖励
jimdempseyatthecove
名誉分销商 III
2,456 次查看

>>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 项奖励
Junru_Zhang
初学者
2,356 次查看

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

0 项奖励
回复