# How do you generate file dependence when writing Makefile?

**URL:** <https://fortran-lang.discourse.group/t/how-do-you-generate-file-dependence-when-writing-makefile/2125>\
**Category:** Uncategorized\
**Created:** [October 20, 2021, 4:55pm UTC](https://fortran-lang.discourse.group/t/how-do-you-generate-file-dependence-when-writing-makefile/2125 "2021-10-20T16:55:39Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![CRquantum](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/crquantum/32/730_2.png) [@CRquantum](https://fortran-lang.discourse.group/u/CRquantum)\
**Post date:** [October 20, 2021, 4:55pm UTC](https://fortran-lang.discourse.group/t/how-do-you-generate-file-dependence-when-writing-makefile/2125/1 "2021-10-20T16:55:39Z")

</div>

Dear all,

May I ask, when writing Makefile, how do you generate file dependence?

For not so many f90 files, I can do it manually, like below.  
But if the number of files are big, like 100 files, it becomes very cumbersome.  
Is there any tool that can generate file dependence automatically?

I know in windows using Code::Block I can generate a Makefile.  
But I would like to hear ideas (in windows or linux) from your guys, many of you are more more experienced and advanced than me.

Thank you very much in advance!

test\_CR3BP.o: FLINT.o test\_CR3BP.f90  
(FC) (FFLAGS) -c test\_CR3BP.f90

FLINT.o: FLINT\_base.o ERK.o FLINT.f90  
(FC) (FFLAGS) -c FLINT.f90

FLINT\_base.o: FLINTUtils.o FLINT\_base.f90  
(FC) (FFLAGS) -c FLINT\_base.f90

FLINTUtils.o: FLINTUtils.f90  
(FC) (FFLAGS) -c FLINTUtils.f90

ERK.o: FLINT\_base.o ButcherTableaus.o StepSz.o ERK.f90  
(FC) (FFLAGS) -c ERK.f90

ERKInit.o: ERK.o ERKInit.f90  
(FC) (FFLAGS) -c ERKInit.f90

ERKIntegrate.o: ERK.o ERKIntegrate.f90  
(FC) (FFLAGS) -c ERKIntegrate.f90

ERKInterp.o: ERK.o ERKInterp.f90  
(FC) (FFLAGS) -c ERKInterp.f90

ERKStepInt.o: ERK.o ERKStepInt.f90  
(FC) (FFLAGS) -c ERKStepInt.f90

ButcherTableaus.o: FLINT\_base.o ButcherTableaus.f90  
(FC) (FFLAGS) -c ButcherTableaus.f90

StepSz.o: FLINT\_base.o StepSz.f90  
(FC) (FFLAGS) -c StepSz.f90

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [October 20, 2021, 5:18pm UTC](https://fortran-lang.discourse.group/t/how-do-you-generate-file-dependence-when-writing-makefile/2125/2 "2021-10-20T17:18:26Z")

</div>

> [@CRquantum](#):
>
> Dear all,
> 
> May I ask, when writing Makefile, how do you generate file dependence?

A related discussion was [Module dependencies](https://fortran-lang.discourse.group/t/module-dependencies/1194) , where people recommended some tools.

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [October 20, 2021, 9:25pm UTC](https://fortran-lang.discourse.group/t/how-do-you-generate-file-dependence-when-writing-makefile/2125/3 "2021-10-20T21:25:27Z")

</div>

Ultimately you can just make it compile with `fpm` (which should work out of the box eventually) and then I want `fpm` to generate such a standalone Makefile that you can use.

---

<div class="post-metadata">

**Author:** ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)\
**Post date:** [October 20, 2021, 10:59pm UTC](https://fortran-lang.discourse.group/t/how-do-you-generate-file-dependence-when-writing-makefile/2125/4 "2021-10-20T22:59:48Z")

</div>

Having written the logic to do this a couple of times, I’ll give some insights, and why Make is not a good tool for this.

File dependency information is a many-to-many relationship. It’s just that the simple cases most people encounter early in their learning happen to only be one-to-one. The general rule is as follows. Compiling a Fortran source file:

- produces:
  - an object file containing the compiled code from that source file
  - a module file (typically `module_name.mod`) for each module in the source file
  - a submodule file (typically `module_name@submodule_name.smod`) for each submodule in the source file

- depends on
  - the module file for each module `use`d in the source file
  - the parent module and parent submodule files for any submodule in the source file

Linking to produce an executable depends on all of the object files containing code that is needed by the program, whether that be modules, submodules, or external subprograms, not to mention the main program.

This is why Make is generally not sufficient. You cannot define a rule that produces multiple targets.

```auto
module_name.mod module_name.o: module_name.f90
    ...

```

does not do what you think it does. It says that this rule could produce one or the other files, not that it produces both in a single execution.

This also has the potential to for circular dependencies. I.e. a source file could contain a module, and be `use`d by other code in the source file. The submodule relationship has the same potential issue.

As you can see, it is thus quite difficult to do this generically, and the reason most projects have a convention of one program unit per source file. (i.e. module, submodule, program, or external procedure).

---

<div class="post-metadata">

**Author:** ![dwwork](https://avatars.discourse-cdn.com/v4/letter/d/71e660/32.png) [@dwwork](https://fortran-lang.discourse.group/u/dwwork)\
**Post date:** [October 21, 2021, 1:54am UTC](https://fortran-lang.discourse.group/t/how-do-you-generate-file-dependence-when-writing-makefile/2125/5 "2021-10-21T01:54:36Z")

</div>

I recommend you not use makefiles. Use fpm or Meson, which can take care of these tricky details for you.

---

<div class="post-metadata">

**Author:** ![MarDie](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/mardie/32/54_2.png) [@MarDie](https://fortran-lang.discourse.group/u/MarDie)\
**Post date:** [October 21, 2021, 11:29am UTC](https://fortran-lang.discourse.group/t/how-do-you-generate-file-dependence-when-writing-makefile/2125/6 "2021-10-21T11:29:40Z")

</div>

> [@CRquantum](#):
>
> May I ask, when writing Makefile, how do you generate file dependence?

I’m used to CMake, my skeleton for small projects is in [GitHub - MarDiehl/fortran-skeleton: Cmake-based fortran skeletons](https://github.com/MarDiehl/fortran-skeleton)

---

<div class="post-metadata">

**Author:** ![leetaylor](https://avatars.discourse-cdn.com/v4/letter/l/3d9bf3/32.png) [@leetaylor](https://fortran-lang.discourse.group/u/leetaylor)\
**Post date:** [October 22, 2021, 1:24am UTC](https://fortran-lang.discourse.group/t/how-do-you-generate-file-dependence-when-writing-makefile/2125/7 "2021-10-22T01:24:18Z")

</div>

> [@everythingfunctional](#):
>
> This is why Make is generally not sufficient. You cannot define a rule that produces multiple targets.
> 
> ```auto
> module_name.mod module_name.o: module_name.f90
> ...
> 
> ```

gmake provides pattern rules which allows multiple targets from a single file.

```auto
%.mod %.o : %.f90

```

The catch is that the stem, `%` , must be identical. So the file `module_name.f90` must contain a module named `module_name`. This also implies that there is only one module per file. I have not tried this with submodules. It looks like it may not be possible to get a common stem since the submodule name is used – `module_name@submodule_name.smod`.

---

<div class="post-metadata">

**Author:** ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)\
**Post date:** [October 22, 2021, 2:17pm UTC](https://fortran-lang.discourse.group/t/how-do-you-generate-file-dependence-when-writing-makefile/2125/8 "2021-10-22T14:17:43Z")

</div>

> [@leetaylor](#):
>
> gmake provides pattern rules which allows multiple targets from a single file.
> 
> ```auto
> %.mod %.o : %.f90
> 
> ```

I’ve looked into this (maybe I’m still mistaken), but this doesn’t mean what you think it does. It doesn’t mean that executing the rule will produce both files. It means that this rule _can_ produce either of these files. At least that’s what make interprets it as. Of course in this case that it was happens, but make doesn’t necessarily expect that.

---

<div class="post-metadata">

**Author:** ![ashe](https://avatars.discourse-cdn.com/v4/letter/a/ce73a5/32.png) [@ashe](https://fortran-lang.discourse.group/u/ashe)\
**Post date:** [November 2, 2021, 12:24am UTC](https://fortran-lang.discourse.group/t/how-do-you-generate-file-dependence-when-writing-makefile/2125/9 "2021-11-02T00:24:52Z")

</div>

This seems to work really well, thanks!

Do you know how cmake figures out the file dependencies? (How cmake works is beyond me.)

---

<div class="post-metadata">

**Author:** ![zoziha](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/zoziha/32/582_2.png) [@zoziha](https://fortran-lang.discourse.group/u/zoziha)\
**Post date:** [November 2, 2021, 12:51am UTC](https://fortran-lang.discourse.group/t/how-do-you-generate-file-dependence-when-writing-makefile/2125/10 "2021-11-02T00:51:39Z")

</div>

It seems that there is a software called [makedepf90](https://linux.die.net/man/1/makedepf90) under Linux that can help generate makefiles, but I can’t find it in Windows-msys2, and I haven’t used it. I just heard people mention this software.

---

<div class="post-metadata">

**Author:** ![mohoree](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@mohoree](https://fortran-lang.discourse.group/u/mohoree)\
**Post date:** [November 2, 2021, 4:42am UTC](https://fortran-lang.discourse.group/t/how-do-you-generate-file-dependence-when-writing-makefile/2125/11 "2021-11-02T04:42:01Z")

</div>

This works very well for me:

> **[Writing Makefiles for Modern Fortran](https://aoterodelaroza.github.io/devnotes/modern-fortran-makefiles/)**
>
> Notes on how to write Makefiles for (modern) fortran programs. Modules, submodules, and automatic dependency generation.

---

<div class="post-metadata">

**Author:** ![MarDie](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/mardie/32/54_2.png) [@MarDie](https://fortran-lang.discourse.group/u/MarDie)\
**Post date:** [November 2, 2021, 6:20am UTC](https://fortran-lang.discourse.group/t/how-do-you-generate-file-dependence-when-writing-makefile/2125/12 "2021-11-02T06:20:28Z")

</div>

> [@ashe](#):
>
> Do you know how cmake figures out the file dependencies? (How cmake works is beyond me.)

I looks like CMake parses the source files and can detect dependencies based on statements like `module`, `submodule`, `use`, and maybe a few more.

---

<div class="post-metadata">

**Author:** ![Jcollins](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/jcollins/32/540_2.png) [@Jcollins](https://fortran-lang.discourse.group/u/Jcollins)\
**Post date:** [November 2, 2021, 7:29am UTC](https://fortran-lang.discourse.group/t/how-do-you-generate-file-dependence-when-writing-makefile/2125/13 "2021-11-02T07:29:52Z")

</div>

fpt generates makefiles ([http://simconglobal.com](http://simconglobal.com)).  
It assumes that modules create .mod and .o files and handles the dependencies separately. But the current version doesn’t handle sub-modules. Working on it.

---

<div class="post-metadata">

**Author:** ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)\
**Post date:** [November 2, 2021, 2:36pm UTC](https://fortran-lang.discourse.group/t/how-do-you-generate-file-dependence-when-writing-makefile/2125/14 "2021-11-02T14:36:14Z")

</div>

On further exploration, it appears I must retract my earlier statement that you can’t have a rule that produces multiple files. I don’t know if this is a new(er) capability or if I just missed it the last time I was trying to research it, but there is a section in the GNU Make manual: [Multiple Targets (GNU make)](https://www.gnu.org/software/make/manual/html_node/Multiple-Targets.html)

Particularly, you’ll want to look at the “Rules with Grouped Targets” which uses `&:` to separate the targets and dependencies. You’ll almost certainly still need a specific rule for each source file, making this a significant maintenance headache, but it’s apparently possible.

But, it being possible is good news for the desire to have `fpm` generate Makefiles.

Also, that means I owe @leetaylor an apology for dismissing his contribution. He is correct that it is possible. Sorry.

---

<div class="post-metadata">

**Author:** ![leetaylor](https://avatars.discourse-cdn.com/v4/letter/l/3d9bf3/32.png) [@leetaylor](https://fortran-lang.discourse.group/u/leetaylor)\
**Post date:** [November 23, 2021, 7:28am UTC](https://fortran-lang.discourse.group/t/how-do-you-generate-file-dependence-when-writing-makefile/2125/15 "2021-11-23T07:28:46Z")

</div>

gmake Version 4.3 (19 Jan 2020) introduced a new feature which can be used to help describe module and submodule dependencies. Documented at [Multiple-Targets](https://www.gnu.org/software/make/manual/html_node/Multiple-Targets.html). The summary from the [NEWS](https://git.savannah.gnu.org/cgit/make.git/tree/NEWS) file.

> New feature: Grouped explicit targets  
> Pattern rules have always had the ability to generate multiple targets with  
> a single invocation of the recipe. It’s now possible to declare that an  
> explicit rule generates multiple targets with a single invocation. To use  
> this, replace the “:” token with “&:” in the rule. To detect this feature  
> search for ‘grouped-target’ in the .FEATURES special variable.  
> Implementation contributed by Kaz Kylheku [kaz@kylheku.com](mailto:kaz@kylheku.com)

I don’t have access to that version of gmake, so I’ve used an intermediate file  
for rules which generate multiple output files. This is described at  
[multiple outputs](https://www.gnu.org/software/automake/manual/html_node/Multiple-Outputs.html)

```auto
%.time : %.f
        $(FC) -c $^
        touch $@

tstsubmodule.time : tstsubmodule.f 
tstsubmodule.o : tstsubmodule.time
$(moduledir)/tstsubmodule.mod : tstsubmodule.time
$(moduledir)/tstsubmodule.smod : tstsubmodule.time

```

A similar technique is used in the link listed in an earlier reply but calls them _anchor_ files [Writing Makefiles for Modern Fortran](https://aoterodelaroza.github.io/devnotes/modern-fortran-makefiles/)

One interesting feature of his technique is the use of two pass compiling. We’ve been using a similar technique and documented by the developer at [A Note on Compiling Fortran](https://dl.acm.org/doi/10.1145/3171572.3171573)

> Some Fortran compilers (Intel - ifort, GNU - gfortran and IBM - xlf,  
> at least) offer an option to ‘‘verify syntax’’, with the side effect  
> of also producing any associated Fortran module files. As it happens,  
> this option usually runs much faster than the object code generation  
> and optimization phases. For some projects on some machines, it can be  
> advantageous to compile in two passes: The first pass generates the  
> module files, quickly; the second pass produces the object files, in  
> parallel. We achieve a 3.8 x speedup in the case study below.

We’re currently moving from gmake to CMake. I don’t believe CMake supports two pass compiling. Instead I’ve been advocating the use of submodule to prevent parallel build times from going back to pre-two-pass build speeds.

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [November 23, 2021, 1:22pm UTC](https://fortran-lang.discourse.group/t/how-do-you-generate-file-dependence-when-writing-makefile/2125/16 "2021-11-23T13:22:40Z")

</div>

> [@leetaylor](#):
>
> We’ve been using a similar technique and documented by the developer at [A Note on Compiling Fortran](https://dl.acm.org/doi/10.1145/3171572.3171573)

A free version of the article is [here](https://www.osti.gov/servlets/purl/1393322).

---

<div class="post-metadata">

**Author:** ![RonShepard](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@RonShepard](https://fortran-lang.discourse.group/u/RonShepard)\
**Post date:** [December 26, 2025, 4:17pm UTC](https://fortran-lang.discourse.group/t/how-do-you-generate-file-dependence-when-writing-makefile/2125/17 "2025-12-26T16:17:10Z")

</div>

> [@everythingfunctional](#):
>
> This is why Make is generally not sufficient. You cannot define a rule that produces multiple targets.
> 
> ```auto
> module_name.mod module_name.o: module_name.f90
> ...
> 
> ```
> 
> does not do what you think it does.

A practical workaround is to not place the `*.mod` files into the dependency process at all. Just list the `*.o` files, and any `*.mod` files that are produced get a free ride. This works because the `*.o` files are always produced and are always needed by the loader, so as long as the dependency/prerequisites are specified correctly for them, everything else must also work correctly.

In hindsight I think it was a mistake in the fortran standard to avoid this issue when f90 introduced modules into the language. This dependency specification became a central issue in the build process at that time, and there should have been some kind of standard mechanism to specify those dependencies. The `make` utility can get the job done, but it is quirky. And if you ever get source code but no accompanying makefile, it can be tedious to reproduce the correct dependencies. The problem with `make` is that in addition to the dependencies (which are universal), it also tries to handle the compiler and compiler options (which are of course compiler, OS, and environment dependent). So make tries to do too much at once, and gets in its own way.
