Valentin Clement
Valentin Clement

Reputation: 231

Use FORTRAN linker in cmake mixed language project

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.

Upvotes: 4

Views: 2055

Answers (2)

marcin
marcin

Reputation: 3581

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

Upvotes: 3

Mike Kinghan
Mike Kinghan

Reputation: 61610

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)

Upvotes: 0

Related Questions