[CMake] linking mixed language executable; cmake choosing wrong compiler

Brad King brad.king at kitware.com
Mon Jan 9 16:28:41 EST 2012


On 1/9/2012 3:44 PM, Mitchell, John A wrote:
> add_executable(demo_cf_pointer demo_cf_pointer.F90)
> target_link_libraries(demo_cf_pointer I_Fortran -lstdc++)

CMake prefers to use the C++ compiler to link when any C++ is involved.
Some toolchains use a "prelinker" step that actually runs the C++ compiler
to handle template instantiations.  IIRC some vendors document that the
C++ compiler should be used if there is any C++ in the link.  This may
or may not still be true on modern tools, but that is the historical
reason for this behavior.

You can tell CMake explicitly to use the Fortran compiler to link:

   set_property(TARGET demo_cf_pointer PROPERTY LINKER_LANGUAGE Fortran)

CMake should still take care of the implicit libraries so you do not need
the explicit "-lstdc++" (though I have not tested this with MPI compiler
wrappers myself).

-Brad


More information about the CMake mailing list