# FPM does not find an external module

**URL:** <https://fortran-lang.discourse.group/t/fpm-does-not-find-an-external-module/5509>\
**Category:** Help\
**Created:** [April 5, 2023, 2:40pm UTC](https://fortran-lang.discourse.group/t/fpm-does-not-find-an-external-module/5509 "2023-04-05T14:40:29Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lufla](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/lufla/32/2836_2.png) [@Lufla](https://fortran-lang.discourse.group/u/Lufla)\
**Post date:** [April 5, 2023, 2:40pm UTC](https://fortran-lang.discourse.group/t/fpm-does-not-find-an-external-module/5509/1 "2023-04-05T14:40:29Z")

</div>

Hi.

I am trying to use FPM to compile a pre-processor for weather forecast data. The toml:

```auto
name = "pre-brams"

version = "1.1.0"

license = "GPL 3.0"

author = "Rodrigues, L.F."

maintainer = "luizfrodrigues@protonmail.com"

copyright = "Copyright 2023, The Brams Team"

[library]

source-dir = "src"

include-dir = ["/opt/gnu/include"]

[build]

auto-executables = true

auto-tests = true

auto-examples = true

external-modules = ["wgrib2api","netcdf"]

link = ["/opt/gnu/include"]

[install]

library = false

```

But when I try to compile, the fpm send me the message:

```auto
 377 | use wgrib2api
      | 1
Fatal Error: Cannot open module file ‘wgrib2api.mod’ for reading at (1)

```

The module wgrib2api is in /opt/gnu/include. Why the fpm don’t found it?

---

<div class="post-metadata">

**Author:** ![FedericoPerini](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/federicoperini/32/1750_2.png) [@FedericoPerini](https://fortran-lang.discourse.group/u/FedericoPerini)\
**Post date:** [April 5, 2023, 3:44pm UTC](https://fortran-lang.discourse.group/t/fpm-does-not-find-an-external-module/5509/2 "2023-04-05T15:44:03Z")

</div>

Hello, @Lufla, welcome to the Discourse!

as per [fpm documentation](https://fpm.fortran-lang.org/en/spec/manifest.html#use-system-installed-modules), I believe you could solve your issue by manually passing `fpm` a compiler flag that tells the compiler to search in that folder, i.e., if you’re using gfortran,

```bash
fpm build --flag -I/opt/gnu/include

```

Does this solve your issue?

Also: please send the output of your `fpm build --verbose` command, that will help with understanding what’s going on.

---

<div class="post-metadata">

**Author:** ![Lufla](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/lufla/32/2836_2.png) [@Lufla](https://fortran-lang.discourse.group/u/Lufla)\
**Post date:** [April 5, 2023, 3:57pm UTC](https://fortran-lang.discourse.group/t/fpm-does-not-find-an-external-module/5509/3 "2023-04-05T15:57:41Z")

</div>

Hi Federico. Thanks for Your help. Using -I It works. 👍

---

<div class="post-metadata">

**Author:** ![Lufla](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/lufla/32/2836_2.png) [@Lufla](https://fortran-lang.discourse.group/u/Lufla)\
**Post date:** [April 5, 2023, 5:05pm UTC](https://fortran-lang.discourse.group/t/fpm-does-not-find-an-external-module/5509/4 "2023-04-05T17:05:02Z")

</div>

Now the problem appears in loader, ☹

```auto
[100%] Compiling...
/usr/bin/ld: I cant localize -l/opt/gnu/include
collect2: error: ld returned 1 exit status
<ERROR> Compilation failed for object " main "
<ERROR>stopping due to failed compilation
STOP 1

```

---

<div class="post-metadata">

**Author:** ![FedericoPerini](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/federicoperini/32/1750_2.png) [@FedericoPerini](https://fortran-lang.discourse.group/u/FedericoPerini)\
**Post date:** [April 5, 2023, 5:21pm UTC](https://fortran-lang.discourse.group/t/fpm-does-not-find-an-external-module/5509/5 "2023-04-05T17:21:25Z")

</div>

The `link` key:

```toml
link = ["/opt/gnu/include"]

```

[should include the library name](https://fpm.fortran-lang.org/en/spec/manifest.html#link-external-libraries), not the folder:

```toml
link = ["wgrib2api","netcdf"]

```

---

<div class="post-metadata">

**Author:** ![Lufla](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/lufla/32/2836_2.png) [@Lufla](https://fortran-lang.discourse.group/u/Lufla)\
**Post date:** [April 5, 2023, 7:36pm UTC](https://fortran-lang.discourse.group/t/fpm-does-not-find-an-external-module/5509/6 "2023-04-05T19:36:25Z")

</div>

I did it. Look the fpm.toml:

```auto
name = "pre-brams"
version = "1.1.0"
license = "GPL 3.0"
author = "Rodrigues, L.F."
maintainer = "luizfrodrigues@protonmail.com"
copyright = "Copyright 2023, The Brams Team"

[library]
source-dir = "src"

[build]
auto-executables = true
auto-tests = true
auto-examples = true
external-modules = ["wgrib2api","netcdf"]
link = ["wgrib2api","netcdf"]
[install]
library = false

```

I run the command using:

`fpm build --profile release --flag -ffree-line-length-none --flag -I/opt/gnu/include`

But I receipt the error:

```auto
modConstants.f90 done.
chem1_list.f90 done.
dump.F90 done.
memoryMod.f90 done.
utilsMod.f90 done.
chemMod.f90 done.
filesMod.f90 done.
engineMod.f90 done.
libpre-brams.a done.
main.F90 done.
main failed.
[100%] Compiling...
/usr/bin/ld: não foi possível localizar -lwgrib2api
/usr/bin/ld: não foi possível localizar -lnetcdf
collect2: error: ld returned 1 exit status
<ERROR> Compilation failed for object " main "
<ERROR>stopping due to failed compilation
STOP 1

```

I don’t know what is wrong.

---

<div class="post-metadata">

**Author:** ![msz59](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@msz59](https://fortran-lang.discourse.group/u/msz59)\
**Post date:** [April 5, 2023, 8:26pm UTC](https://fortran-lang.discourse.group/t/fpm-does-not-find-an-external-module/5509/7 "2023-04-05T20:26:03Z")

</div>

I know nothing about fpm but it seems that it treats `wgrib2api` and `netcdf` as libraries to link with, not the modules which are special object files (you should have both .mod and .o file).

```plaintext
`/usr/bin/ld: não foi possível localizar -lwgrib2api`

```

means that the linker was instructed to find libwgrib2api.a (static) or libwgrib2api.so (dynamic) library file.  
What if you remove the `link =` directive?

---

<div class="post-metadata">

**Author:** ![Lufla](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/lufla/32/2836_2.png) [@Lufla](https://fortran-lang.discourse.group/u/Lufla)\
**Post date:** [April 5, 2023, 9:26pm UTC](https://fortran-lang.discourse.group/t/fpm-does-not-find-an-external-module/5509/8 "2023-04-05T21:26:06Z")

</div>

Thanks for comments msz59. I try two approaches:

1. Copy the libraries from /opt/gnu/lib to /opt/gnu/include (keeping the link directive). Nothing happens and got the same error.

2. Coment the #link = [“wgrib2api”,“netcdf”]. Then the results are worst. The compiler list all the references to the library as “not defined”. (“referência não definida para” = “reference not defined to”. Bellow I put part of output.

```auto
/usr/bin/ld: filesMod.f90:(.text+0x93df): referência não definida para "__netcdf_MOD_nf90_get_var_3d_fourbytereal"
/usr/bin/ld: filesMod.f90:(.text+0x97ce): referência não definida para "__netcdf_MOD_nf90_close"
/usr/bin/ld: filesMod.f90:(.text+0x9e7d): referência não definida para "__netcdf_MOD_nf90_get_var_3d_fourbytereal"
/usr/bin/ld: filesMod.f90:(.text+0xa2b8): referência não definida para "__netcdf_MOD_nf90_get_var_3d_fourbytereal"
/usr/bin/ld: filesMod.f90:(.text+0xa69e): referência não definida para "__netcdf_MOD_nf90_get_var_3d_fourbytereal"
/usr/bin/ld: filesMod.f90:(.text+0xaa6e): referência não definida para "__netcdf_MOD_nf90_get_var_3d_fourbytereal"
/usr/bin/ld: build/gfortran_6ED43C0A746540CF/pre-brams/libpre-brams.a(src_filesMod.f90.o): na função "__filesmod_MOD_readatmosgrib2":
filesMod.f90:(.text+0xd4f0): referência não definida para "__wgrib2api_MOD_grb2_mk_inv"
/usr/bin/ld: filesMod.f90:(.text+0xd5d4): referência não definida para "__wgrib2api_MOD_grb2_inq"
/usr/bin/ld: filesMod.f90:(.text+0xd852): referência não definida para "__wgrib2api_MOD_grb2_inq"
/usr/bin/ld: filesMod.f90:(.text+0xddb2): referência não definida para "__wgrib2api_MOD_grb2_inq"
/usr/bin/ld: filesMod.f90:(.text+0xddf0): referência não definida para "__wgrib2api_MOD_grb2_free_file"
/usr/bin/ld: filesMod.f90:(.text+0xf4d0): referência não definida para "__wgrib2api_MOD_grb2_inq"
/usr/bin/ld: filesMod.f90:(.text+0xfad8): referência não definida para "__wgrib2api_MOD_grb2_free_file"

```

Actually I think that FPM is not good enough for the proposal I want. As base for compile a large model. But I will keep trying.

---

<div class="post-metadata">

**Author:** ![FedericoPerini](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/federicoperini/32/1750_2.png) [@FedericoPerini](https://fortran-lang.discourse.group/u/FedericoPerini)\
**Post date:** [April 6, 2023, 5:40am UTC](https://fortran-lang.discourse.group/t/fpm-does-not-find-an-external-module/5509/9 "2023-04-06T05:40:07Z")

</div>

> [@Lufla](#):
>
> ```auto
> external-modules = ["wgrib2api","netcdf"]
> link = ["wgrib2api","netcdf"]
> 
> ```

Hi @Lufla, I think you’ve got these right now:

- `external-modules` tells fpm that there are `use`d modules with those names, which are not in the project’s source files;
- `link` tells fpm that it needs to link `libwgrib2api.a` and `libnetcdf.a`
- `-I/opt/gnu/include` tells fpm where the additional module files are

Now I think you should also ensure your libraries are seen by the compiler.

1. Option 1: add their path to environment variable `LD_LIBRARY_PATH`
2. Option 2: pass their path to `fpm` by adding one more flag: `--flag -L/opt/gnu/lib` (or the exact path where the `.a` files are located

`fpm` was meant more as a package manager (i.e., closed ecosystem) than a full build system, but if you’ve got suggestions, please submit them to the project’s [issue page](https://github.com/fortran-lang/fpm/issues) so the community can discuss over them!

PS: also note I’m undergoing a [metapackages](https://github.com/fortran-lang/fpm/issues/853) implementation that should soon make the most common dependencies automatic (think coarrays, MPI, HDF, netCDF, etc.).

---

<div class="post-metadata">

**Author:** ![hkvzjal](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/hkvzjal/32/3055_2.png) [@hkvzjal](https://fortran-lang.discourse.group/u/hkvzjal)\
**Post date:** [April 6, 2023, 6:16am UTC](https://fortran-lang.discourse.group/t/fpm-does-not-find-an-external-module/5509/10 "2023-04-06T06:16:47Z")

</div>

After reading this post and having had a very similar issue I took the freedom of opening a discussion in the issue page [Could fpm evolve into an "open-ecosystem" build tool? · Issue #866 · fortran-lang/fpm · GitHub](https://github.com/fortran-lang/fpm/issues/866) just in case it could be taken into consideration 🙂

---

<div class="post-metadata">

**Author:** ![Lufla](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/lufla/32/2836_2.png) [@Lufla](https://fortran-lang.discourse.group/u/Lufla)\
**Post date:** [April 6, 2023, 10:24am UTC](https://fortran-lang.discourse.group/t/fpm-does-not-find-an-external-module/5509/11 "2023-04-06T10:24:07Z")

</div>

Hi @FedericoPerini ,

One more time, thanks a lot! I had a huge mistake here. The problem was another libraries: wgrib2\_api and netcdff (NetCDF Fortran) that not included in link list. I put them and fix the issue. The lines became:

```auto
 external-modules = ["wgrib2api","netcdf"]
link = ["wgrib2","wgrib2_api","netcdf","netcdff"]

```

For compile command i did:

`fpm build --profile release --flag -ffree-line-length-none --flag -I/opt/gnu/include --flag -L/opt/gnu/lib`

and voilà!

But, as wrote @hkvzjal the ideal is to have all ecosystem inside FPM. Future?

Regards!
