[CMake] linking main Fortran and C++

Michael Scot Breitenfeld brtnfld at uiuc.edu
Mon Oct 18 21:43:36 EDT 2010


      My main program is in Fortran and I have couple of
      files that are in C++. When I try to make the library,
      CMake uses CXX linking (archiving) to make the library.

      Which I guess is ok, but then when it links the main program it
      thinks that it is a CXX executable and uses the C++ compiler and
      not the Fortran compiler so compilation fails.

      How do I tell cmake to use fortran instead, I
      thought it would automatically do this from the .f90 suffix. I'm
      using cmake 2.8.1.

      Thanks, Scot

My test:

PROJECT( Test )

SET(PACKAGE_NAME "TEST")

CMAKE_MINIMUM_REQUIRED (VERSION 2.6)

SET (CMAKE_Fortran_COMPILER  gfortran)
SET (CMAKE_CC_COMPILER  gcc)
SET (CMAKE_CXX_COMPILER  g++)

# libraries are all shared by default
option(BUILD_SHARED_LIBS "Build shared libraries" OFF)

ENABLE_LANGUAGE(Fortran)

SET(F_OBJ submain.f90)

SET(C_OBJ test.cpp)

ADD_LIBRARY (name STATIC ${F_OBJ} ${C_OBJ})

ADD_EXECUTABLE(a.out main.f90)

TARGET_LINK_LIBRARIES(a.out name)


More information about the CMake mailing list