0

I am using gfortran (in the form of mpif90) to link a large mpi program. It all seems to be fine except for one library libgptl.a.

The link step generates a number of errors but all of the form:

libgptl.a(perf_mod.o): in function `__perf_mod_MOD_t_stopf':perf_mod.F90:(.text+0x844): undefined reference to `gptlstop_'

or

perf_mod.F90:(.text+0xa76): undefined reference to `gptlstop_'

If I try "nm libgptl.a" to see what is in the static library, the relevant lines appear to be:

gptl.o:                         [From gptl.c]
...
0000000000003b76 T GPTLstop
0000000000004040 T GPTLstopf
00000000000042ca T GPTLstopf_handle
0000000000003db9 T GPTLstop_handle
0000000000003a2a T GPTLstop_instr
...

f_wrappers.o                    [From f_wrappers.c]
...
0000000000000218 T gptlstop
                 U GPTLstopf
                 U GPTLstopf_handle
0000000000000226 T gptlstop_handle
...

perf_mod.o                    [From perf_mod.F90]
...
                 U gptlstop_
...
00000000000006c7 T __perf_mod_MOD_t_stopf

I guess that the problem is something to do with gfortran placing the underscore at the end of the name.

Is there a simple way to help the linker make the correct connection between names with and without underscores (i.e. gptlstop_ in perf_mod.o and gptlstop in f_wrappers.o) or make the earlier compile step use a more consistent naming?

7
  • There are various options that control this. However, they depend on what is in the code in the first place. Please show the relevant definitions from the code and the commands you currently use for the compilations and linking (of all the relevant files). If you call a C function, it is by far best to call it using bind(C,name="..."). See fortran-iso-c-binding. Commented Nov 29, 2023 at 15:15
  • Thanks for getting back. I've checked the routines mentioned and there is no attempt to use the 'bind(c)' element or use 'iso-c_binding'. I'll get back to the coders and find out why these elements are missing. Some of the code looks old and may predate Fortran 2003.
    – David Webb
    Commented Nov 29, 2023 at 17:01
  • As I wrote, there are compiler options that might be used here, but we must see what is in the code and how it is currently compiled. They must be use carefuly so that they don't break something else. Commented Nov 29, 2023 at 17:58
  • The code is a bit messy and one of the files involved is 150k, so I am a bit wary about uploading them. Following up your note about bind(c), one of the C routines has a series of ifdef/elif section where one of the options is -DFORTRANUNDERSCORE. This option includes lines like "#define gptlstop gptlstop_", supporting the idea that the code predates Fortran 2003. I added the option to the environment variable CFLAGS, tried again and the library linked correctly. However I still have undefined references to "chdir_" and "system_". I presume these are system calls with a similar problem.
    – David Webb
    Commented Nov 29, 2023 at 20:08
  • Both SYSTEM and CHDIR are typically available as intrinsics, you should not need to link them this way if they are called from Fortran. gcc.gnu.org/onlinedocs/gfortran/CHDIR.html gcc.gnu.org/onlinedocs/gfortran/SYSTEM.html If they are declared EXTERNAL then you are on your own, but I would just check if the calls conform to the intrinsics and remove the EXTERNAL. Commented Nov 29, 2023 at 20:19

0