How to pass compiling and linking flags to fpm

I have a local library and module files not in current working directory. I need compile and link a new project to this library using fpm. Where should I modify the fpm.toml or pass the flag to the fpm command?

See Manifest reference — Fortran Package Manager (specifying-dependencies)

You can also use the --flag option, for example:

$ fpm run --flag '$(pkg-config --cflags mylibrary)'

The reason I am asking for help here is that it is difficult for me to understand some description in Manifest reference. Maybe I need some tutorials on how to link local library to a project. Anyway, here is my situation: I have a local library libmylib.a at ~/mine/lib and the related mod files at ~/mine/include. Based on Manifest:link external library, the build section is modified as

link = "mylib"

However, where do I specify the the path for lib and mods? When compile and link using gfortran, the command used was
gfortran -I~/mine/include -L~/mine/lib main.f90 -lmylib

There are a few different ways that this can be done in fpm.

  • If you have local includes specify them in the "include-dir" = ["mylocalinc"] keyword. Keep in mind this

include-dir does not currently allow using pre-built module .mod files

  • If you have includes outside of your project either:
    • Use the fpm subcommand --flag to pass whatever compiler options you want to fpm
    • Set the shell variable FPM_FFLAGS to pass whatever compiler options you want to fpm
    • Or last option, that I do not recommend but I list for completeness, append/prepend to the variable your compiler uses to look for include paths for GNU (gfortran) that is CPATH e.g. CPATH=${CPATH}:/my/path

For linking the options that you have are pretty much the same. In your fpm.toml add the lib you wish to link against to

link = ["mylib"]
  • Use the fpm subcommand --link-flag e.g. fpm build --link-flag "-L/path/to/mylib"
  • Set FPM_LDFLAGS e.g. export FPM_LDFLAGS="-L/path/to/mylib" and then fpm build
  • Or set the compiler variables LD_LIBRARY_PATH (and for gfortran LIBRARY_PATH) to include your “/path/to/mylib” e.g. export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/path/to/mylib, export LIBRARY_PATH=${LIBRARY_PATH}:/path/to/mylib and then fpm build

Here is an example where you link against an external library is GitHub - gnikit/gmsh-fpm: Gmsh API using the Fortran Package Manager (fpm)

1 Like

Thank you very much. Very clear description.