# Where to package mod files?

**URL:** <https://fortran-lang.discourse.group/t/where-to-package-mod-files/5024>\
**Category:** Help\
**Created:** [January 13, 2023, 8:44am UTC](https://fortran-lang.discourse.group/t/where-to-package-mod-files/5024 "2023-01-13T08:44:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mkitti](https://avatars.discourse-cdn.com/v4/letter/m/838e76/32.png) [@mkitti](https://fortran-lang.discourse.group/u/mkitti)\
**Post date:** [January 13, 2023, 8:44am UTC](https://fortran-lang.discourse.group/t/where-to-package-mod-files/5024/1 "2023-01-13T08:44:58Z")

</div>

I’m trying to help package version 1.14.0 of the HDF5 library via MSYS2 and MINGW.

There’s a question of where to place `.mod` files when packaging.

Previously, they were placed in `bin/static/Release` and `bin/shared/Release`. It’s been suggested that they be moved to the `include` folder. I do not have much experience with this, so I would appreciate your insight.

> <https://github.com/msys2/MINGW-packages/pull/15033>
>
> This adapts the mingw-w64-hdf5 script for version 1.14.0 of the HDF5 C library.
> …
> 
> Note that a 1.12.3 release is also expected in the near future.
> 
> Patch hdf5-fix-pkgconfig.patch was \[already applied upstream\](https://github.com/HDFGroup/hdf5/commit/dcccc355261e305a1d877a798d7fd46556b3cbce).

---

<div class="post-metadata">

**Author:** ![Arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Arjen](https://fortran-lang.discourse.group/u/Arjen)\
**Post date:** [January 13, 2023, 2:39pm UTC](https://fortran-lang.discourse.group/t/where-to-package-mod-files/5024/2 "2023-01-13T14:39:19Z")

</div>

Just my two cents:

- .mod files are specific to the compiler, so in that sense they belong more to the libraries.
- But they are needed at compile time, which makes them more like include files.

To solve this puzzle I suggest you look at the solution fpm uses.

---

<div class="post-metadata">

**Author:** ![plevold](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/plevold/32/754_2.png) [@plevold](https://fortran-lang.discourse.group/u/plevold)\
**Post date:** [January 13, 2023, 2:44pm UTC](https://fortran-lang.discourse.group/t/where-to-package-mod-files/5024/3 "2023-01-13T14:44:12Z")

</div>

As @Arjen says, .mod files are specific to both a compiler vendor **and** a compiler version. So the short answer is that you don’t package them.

---

<div class="post-metadata">

**Author:** ![gnikit](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/gnikit/32/1213_2.png) [@gnikit](https://fortran-lang.discourse.group/u/gnikit)\
**Post date:** [January 13, 2023, 7:06pm UTC](https://fortran-lang.discourse.group/t/where-to-package-mod-files/5024/4 "2023-01-13T19:06:08Z")

</div>

> [@plevold](#):
>
> short answer is that you don’t package them

So how would linking to a library compiled with a set of flags _X_ to a code compiled with a set of flags _Y_ work then?

AFAIK when packaging people tend to cover multiple compiler vendors, compiler versions and other external libraries like MPI.

---

<div class="post-metadata">

**Author:** ![plevold](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/plevold/32/754_2.png) [@plevold](https://fortran-lang.discourse.group/u/plevold)\
**Post date:** [January 13, 2023, 7:25pm UTC](https://fortran-lang.discourse.group/t/where-to-package-mod-files/5024/5 "2023-01-13T19:25:18Z")

</div>

To be sure that it works you’d have to make a `bind(c)` interface to the code. The static archive could be packaged with a source file declaring the `interface` in order to make it easy and safe to use.

This isn’t unique to Fortran, but applies to e.g. C++ as well. One reason for this is that different compilers have different name mangling schemes, but the memory layout for types can also be different. When using MSVC (possibly others as well) you can’t pass a `std::string` between code compiled in release mode to code compiled in debug mode because the memory layout will be different.

---

<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:** [January 13, 2023, 7:37pm UTC](https://fortran-lang.discourse.group/t/where-to-package-mod-files/5024/6 "2023-01-13T19:37:44Z")

</div>

The approach I use is to set up a shared module directory, and put all modules that are shared by programs in several directories into that directory. When compiled, these create one or more .mod files in that directory and also a .o file in that directory. I usually move the .o file into a library (.a file), which can then be referenced by the programs that use those shared modules. Of course, if the programs that share the modules are all in the same directory, then you usually don’t need to do anything, the compiler and linker find both the .mod files and the .o files automatically.

This is a little more complicated than libraries that do not have .mod files. If the library has only .o files, then you can move them into libraries and move them to other directories pretty freely. If the library has .mod files, then these files must be available at compile time for all the programs that reference them, whereas the .o files only need to be available at load time.

At one time back in the 1990s I remember a compiler that allowed you to place the .mod files also into a library, which was searched at compile time. I’m unsure if that is still possible. I did not use that feature because it was not portable to all the compilers I was using at that time.

---

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [January 13, 2023, 10:35pm UTC](https://fortran-lang.discourse.group/t/where-to-package-mod-files/5024/7 "2023-01-13T22:35:21Z")

</div>

Previous (inconclusive) discussion on this topic: [Installing Fortran module files](https://fortran-lang.discourse.group/t/installing-fortran-module-files/1135)
