4

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.

Valentin Clement
  • 241
  • 3
  • 13

2 Answers2

3

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).

marcin
  • 3,351
  • 1
  • 29
  • 33
0

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)
Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182
  • Sure, I know this hack, thanks anyway. I was wondering if there is a nicer way to perform this in cmake. I'm using various compiler suite (Cray, PGI, GNU, ...) and it's a pain in the ass to modify the CMAKE_CXX_LINK_EXECUTABLE. Cmake should use the CMAKE_Fortran_LINK_EXECUTABLE in that case but I guess it is not a feature yet. – Valentin Clement Nov 24 '16 at 14:06