Calling procedure from included .mod file, without explicit `use` statement

Ok then, let’s disclose some details to make easier to follow:

  • the incriminated procedure is called bracket, and called in my test code as call bracket(ax,xx,bx,fa,fx,fb,func)

  • the library I’m including via pkg-config is called scifor and the module that defines bracket is called sf_optimize

Very similarly to what @kargl has shown if I inspect the include folder containing all the .mod files of the scifor library, I get

$ cat *.mod | gunzip | grep bracket
3735 'bracket' 'sf_optimize' '' 1 ((PROCEDURE UNKNOWN-INTENT MODULE-PROC
3733 'boltzmann_constant_in_inverse_meters_per_kelvin' 0 3734 'bracket'
50 'bracket' 'sf_optimize' '' 1 ((PROCEDURE UNKNOWN-INTENT MODULE-PROC
('bisect' 0 42 'bracket' 0 50 'brent' 0 59 'brentq' 0 66 'broyden1' 0 73

While if I use nm to inspect the library file, I get

$ nm libscifor.a | grep bracket
000000000002bd60 T __sf_optimize_MOD_bracket
000000000001bbc0 T r8vec_bracket_

Finally if I inspect all the .o files as correctly compiled with either the makefile or fpm build --flag "$(pkg-config --cflags scifor) $(pkg-config --libs scifor)" I get

$ nm *.o | grep bracket
                 U bracket_

More specifically

$ nm src_optimize_cgfit_routines.f90.o | grep bracket
                 U bracket_

where optimize_cgfit_routines.90 is the source file for the modules that has calls to bracket.

If I understand correctly what nm is telling me here, my test code lacks a definition for the symbol bracket_ (why the underscore?), so at linking stage something has to be found elsewhere. The library file libscifor.a has not a matching symbol, due to mangling (right?) so it should be coming from there. The included .mod file for sf_optimize does instead have a bracket symbol indeed, so I’d expect it to be linked.

Crosscheck:

[gbellomi@login2 Test_CGfit]$ fpm build --flag "$(pkg-config --cflags scifor) $(pkg-config --libs scifor)"
minimize_krauth.f                      done.
optimize_cgfit_routines.f90            done.
minimize_sascha.f                      done.
VARS_GLOBAL.f90                        done.
OPTIMIZATION.f90                       done.
BATH_AUX.f90                           done.
BATH_FIT.f90                           done.
libCGfit.a                             done.
[100%] Project compiled successfully.

[gbellomi@login2 Test_CGfit]$ yes | fpm clean
Delete build, excluding dependencies (y/n)?  + rm -rf build/gfortran_07A77609A9445D9C

[gbellomi@login2 Test_CGfit]$ fpm build --flag "$(pkg-config --libs scifor)"
minimize_krauth.f                      done.
optimize_cgfit_routines.f90            done.
minimize_sascha.f                      done.
VARS_GLOBAL.f90                        failed.
[ 50%] Compiling...
././src/VARS_GLOBAL.f90:2:6:

   USE SF_CONSTANTS, only: zero
      1
Fatal Error: Cannot open module file ‘sf_constants.mod’ for reading at (1): No such file or directory
compilation terminated.
<ERROR> Compilation failed for object " src_VARS_GLOBAL.f90.o "
<ERROR>stopping due to failed compilation
STOP 1

[gbellomi@login2 Test_CGfit]$ yes | fpm clean
Delete build, excluding dependencies (y/n)?  + rm -rf build/gfortran_FEBAE927C8C8AD11

[gbellomi@login2 Test_CGfit]$ fpm build --flag "$(pkg-config --cflags scifor)"
minimize_krauth.f                      done.
optimize_cgfit_routines.f90            done.
minimize_sascha.f                      done.
VARS_GLOBAL.f90                        done.
OPTIMIZATION.f90                       done.
BATH_AUX.f90                           done.
BATH_FIT.f90                           done.
libCGfit.a                             done.
[100%] Project compiled successfully.
  • if I do not pass the --cflags pkg-config output to fpm I cannot compile anything, since my code uses some of the modules from scifor (unfortunately it errors before we can see if it would find bracket or not)

  • if I skip instead the --libs entry I get everything working (at least at build level), as much as it did by passing both… I believe this should be enough to prove that bracket is found in the .mod files, not in the library file(s).

To be clear, there is more than just libscifor.a in the --libs output, and I have not looked for the presence of bracket inside the MKLs etc, but the above test should rule out them having any effect on this:

$ pkg-config --libs scifor
-lscifor -L/opt2/sissa/compiler/intel/2020.4/compilers_and_libraries_2020.4.304/linux/mkl/lib/intel64 -lmkl_gf_lp64 -lmkl_sequential -lmkl_core -L/usr/lib64 -lpthread -lm -ldl