-2

I am new to CMake, and have a (legacy) package with a C main program that links (interdependent) C and Fortran libraries. The main program source is in the top-level directory, the C library source is in a sub-directory of the top-level directory, and the Fortran library source is in another (peer) sub-directory. Oversimplified, it looks like:

+-- main.c
+-- mmf
|   +-- (C source)
+-- prms
    +-- (Fortran source)

I have one CMakeLists.txt file in each directory, for a total of three. They are here, here, and here.

Everything appears to work well until the final linking stage, when:

[100%] Linking C executable PRMS
/usr/bin/ld: prms/libprms.a(ccsolrad.f90.o): in function `ccsolrad_':
ccsolrad.f90:(.text+0xbe0): undefined reference to `declvar_'
/usr/bin/ld: ccsolrad.f90:(.text+0xc4b): undefined reference to `declvar_'
/usr/bin/ld: ccsolrad.f90:(.text+0xe1d): undefined reference to `declvar_'
/usr/bin/ld: ccsolrad.f90:(.text+0xe88): undefined reference to `declvar_'

which is some Fortran code in the Fortran library attempting to link with a Fortran-to-C adapter function in the C library.

Would anyone have ideas on how to do this correctly?

Thanks very much!

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
Andy Halper
  • 123
  • 3
  • Show the linking command. Rerun make with `VERBOSE=1`. – KamilCuk Sep 09 '19 at 23:30
  • ```[100%] Linking C executable PRMS /usr/bin/cmake -E cmake_link_script CMakeFiles/PRMS.dir/link.txt --verbose=1 /usr/bin/cc -rdynamic CMakeFiles/PRMS.dir/main.c.o -o PRMS mmf/libmmf.a -lm prms/libprms.a -lgfortran -lgfortran -lm -lquadmath -lm /usr/bin/ld: prms/libprms.a(ccsolrad.f90.o): in function `ccsolrad_': ccsolrad.f90:(.text+0xbe0): undefined reference to `declvar_' /usr/bin/ld: ccsolrad.f90:(.text+0xc4b): undefined reference to `declvar_'``` – Andy Halper Sep 10 '19 at 00:18
  • Maybe I am not right. What if you would link with the libraries twice `target_link_libraries(PRMS mmf m prms gfortran mmf m prms gfortran)` ? Also you can check that `libmmf.a` really has `declvar_` symbol. – KamilCuk Sep 10 '19 at 00:23
  • For the future: On Stack Overflow we want the **code** to be **in the question post** itself, not *linked*. Among other things, this allows the question to remain a valid even after linked code has been changed (as in your case, when the current code corresponds to the fixed problem) or even deleted. – Tsyvarev Sep 10 '19 at 08:17

1 Answers1

1

The library prms uses symbols exported by mmf. You need to link prms with mmf.

Add:

target_link_libraries(prms PUBLIC mmf)

to this.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111