FPM does not find an external module

Hi.

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

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:

 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?

Hello, @Lufla, welcome to the Discourse!

as per fpm documentation, 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,

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.

1 Like

Hi Federico. Thanks for Your help. Using -I It works. :+1:

1 Like

Now the problem appears in loader, :frowning:

[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

The link key:

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

should include the library name, not the folder:

link = ["wgrib2api","netcdf"]

I did it. Look the fpm.toml:

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:

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.

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).

`/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?

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.

/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.

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

  • external-modules tells fpm that there are used 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 so the community can discuss over them!

PS: also note I’m undergoing a metapackages implementation that should soon make the most common dependencies automatic (think coarrays, MPI, HDF, netCDF, etc.).

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 just in case it could be taken into consideration :slight_smile:

1 Like

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:

 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!

1 Like