I am a newbie in fortran and I am trying to understand all I need for the compilation. Until now everything is good but there is something I don't get. Supposing that I modify the code but my modifications wouldn't change the content of the .mod files, am I able to compile and create the object file without recreating the .mod file, and if yes how should I do it. I tried to search on the internet but I didn't really manage to find the answer. By the way, I am using gfortran. Thank you in advance and I am sorry if the answer already exists (I couldn't find it).
Asked
Active
Viewed 811 times
3
-
Are you using any build system such as a Makefile or something similar? Are you asking how to avoid unneded recompilation? Can you show an example (not thhe code, just commands) of what you are doing? – Vladimir F Героям слава Jun 22 '21 at 08:59
-
@VladimirF Unfortunaly I cannot show an example, but that's exactly what I wand to do, avoid unneeded recompilation since it's a huge program and in total it takes approximately 4 minutes to compile. Yes a Makefile is being used! – supe345 Jun 22 '21 at 11:07
-
In that case you do have to show an example. See [ask] and [mcve]. Be aware that there are other more modern build systems, some targeted specifically to Fortran. Basically this is an XY problem, you do not actually need to avoid recreating a mod file, you need to avoid the recompilation. – Vladimir F Героям слава Jun 22 '21 at 11:30
-
@VladimirF you are right I should show part of it but unfortunately it is confidential and it is a program used by a lot of people so it won't be possible at least not in the near future to use another build system. I am only doing an internship for 2 months and I don't have the right to say much – supe345 Jun 22 '21 at 12:10
1 Answers
3
As you're using gfortran, the compiler already does this for you. When compiling a module, it will create the .mod
file under a temporary name, then it will compare checksums between the existing .mod
file (if it exists) and the new one, and replace the existing one with the new one only if the checksums differ.

janneb
- 36,249
- 2
- 81
- 97
-
Thank you very much for your answer.Alright but is there a way to tell gfortran to not create these `.mod` at all? I do not waste time on the recreation of the `.mod` files – supe345 Jun 23 '21 at 07:37
-
Have you profiled and verified that writing that `.mod` file takes up a significant portion of the entire compilation time for that source file? – janneb Jun 23 '21 at 08:58
-
No I haven't tested that yet and I can't find how to generate only the `.mod` files :'( The total compilation time is 4m19s approx – supe345 Jun 23 '21 at 11:32
-
The problem though is that the code is developed by phd students and researchers and they need to compile many times sometimes and it can be a real "waste" of time. That's why they need to reduce the time to the minimum – supe345 Jun 23 '21 at 11:37
-
If you want to only generate the `.mod` file but not the `.o`, thus saving some time, you can use the `-fsyntax-only` option. – janneb Jun 23 '21 at 19:03
-
One usecase for using `-fsyntax-only` to speed up compilation would be to first do one pass with that, thus generating all the `.mod` files needed, then a second pass doing the "real" build which can then be done with more parallelism. Whether that is worth it depends on the project. – janneb Jun 23 '21 at 19:05
-
More generally, to speedup builds, make sure you have the dependencies in the makefile correct. In particular, if you specify the dependencies on `.mod` files rather than the `.o` files, the gfortran feature mentioned in this answer helps avoid recompilation cascades. Another common trick is to have a 'debug build' option that enables various error checking options and has much lower optimization level, enabling faster builds (at the cost of runtime performance). – janneb Jun 23 '21 at 19:07
-