4
votes

When CMake is used for a mixed language project (C/C++ and FORTRAN), the C++ compiler is called to link the executable. Is there an easy way to call the FORTRAN compiler for the linking step.

project(Serialbox_Fortran_Perturbation_Example CXX Fortran)

add_executable(main_producer main_producer.f90 m_ser.f90)

This will compile correctly with the FORTRAN compiler but for the linking step, the C++ compiler will be called and it causes trouble with some compiler suite like PGI for example.

2

2 Answers

3
votes

As a workaround one can set the linker language explicitly:

set_property(TARGET your_target PROPERTY LINKER_LANGUAGE Fortran)

or play with CMAKE_<LANG>_LINKER_PREFERENCE (I haven't checked if the latter works now, it didn't work when I tried a few years ago).

0
votes

I expect what you are seeing is that the linkage is executed through the GCC C++ frontend with Fortran libraries added. To get the linkage done through the GCC Fortran frontend this hack should do:

project(Serialbox_Fortran_Perturbation_Example CXX Fortran)
set(CMAKE_CXX_LINK_EXECUTABLE "gfortran <FLAGS> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS>  -o <TARGET> <LINK_LIBRARIES>")
add_executable(main_producer main_producer.f90 m_ser.f90)