Search code examples
makefilefortrangnu-makegfortran

How to determine the library pointed to by lgfortran


I am trying to compile a build for c++ and Fortran using GNU make. On some machines the build works, on others I get the error undefined reference to '_gfortran_stop_numeric_f08'. I reckon there is some issue with the fortran library, and I am trying to figure this out.

On the machines where the compilation works, the gfortran version is "GNU Fortran (SUSE Linux) 4.8.5". In /usr/lib64/ I have the following library files:

libgfbgraph-0.2.so.0 libgfortran.so.3 libgfortran.so.4 libgfbgraph-0.2.so.0.0.0 libgfortran.so.3.0.0 libgfortran.so.4.0.0

On these machines, compiling with the LDFlag -lgfortran works fine. However, compiling with either -L/usr/lib64/libgfortran.so.4 or -L/usr/lib64/libgfortran.so.3 gives a long list of errors, of the form

/usr/lib64/gcc/x86_64-suse-linux/4.8/../../../../x86_64-suse-linux/bin/ld: io.f90:(.text+0x888d): undefined reference to `_gfortran_st_write'

It seems then that using the flag -lgfortran I am not pointing to either -L/usr/lib64/libgfortran.so.4 or -L/usr/lib64/libgfortran.so.3. Howe can I figure out the library which is actually pointed to?

On the other machines, the compilation does not work even with the flag -lgfortran. This is where I get the error undefined reference to '_gfortran_stop_numeric_f08'. On these machines, the gfortran version is GNU Fortran (SUSE Linux) 7.5.0". In /usr/lib64/ I have the following library files:

libgfortran.so.4 libgfortran.so.4.0.0

Any ideas about how to resolve this?


Solution

  • The library linked when providing -lgfortran will always be libgfortran.so. Most commonly this is a symbolic link to an actual file, which will be later referenced by compiled binary.

    Now, since this is a compiler-provided library, its location can be determined by calling compiler, e.g.:

    # Custom compiler
    $ gfortran --version
    GNU Fortran (GCC) 10.2.0
    Copyright (C) 2020 Free Software Foundation, Inc.
    
    $ gfortran --print-file libgfortran.so
    /proj/subcm/tools/Linux-x86_64/Santiago/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../lib64/libgfortran.so
    
    $ readlink -f $(gfortran --print-file libgfortran.so)
    /proj/subcm/tools/Linux-x86_64/Santiago/lib64/libgfortran.so.5.0.0
    
    
    # Stock distribution compiler
    $ /usr/bin/gfortran --version
    GNU Fortran (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)
    Copyright (C) 2015 Free Software Foundation, Inc.
    
    $ /usr/bin/gfortran --print-file libgfortran.so
    /usr/lib/gcc/x86_64-redhat-linux/4.8.5/libgfortran.so
    
    $ readlink -f $(/usr/bin/gfortran --print-file libgfortran.so)
    /usr/lib64/libgfortran.so.3.0.0