- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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!
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I am not a Cmake user; perhaps, Cmake expects that include files are to be given a distinct suffix such as .fi or .inc ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
>>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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks for your reply. I will carefully read and study from these websites.

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page