[CMake] linking main Fortran and C++

Michael Scot Breitenfeld brtnfld at uiuc.edu
Tue Oct 19 00:35:33 EDT 2010


Thanks, this works now:

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})
SET_TARGET_PROPERTIES(name PROPERTIES LINKER_LANGUAGE Fortran)

ADD_EXECUTABLE(a.out main.f90)
SET_TARGET_PROPERTIES(a.out PROPERTIES LINKER_LANGUAGE Fortran)
TARGET_LINK_LIBRARIES(a.out name)


OUTPUT:

Scanning dependencies of target name
[ 33%] Building Fortran object CMakeFiles/name.dir/submain.f90.o
[ 66%] Building CXX object CMakeFiles/name.dir/test.cpp.o
Linking Fortran static library libname.a
[ 66%] Built target name
Scanning dependencies of target a.out
[100%] Building Fortran object CMakeFiles/a.out.dir/main.f90.o
Linking Fortran executable a.out
[100%] Built target a.out

Added bonus is it links -lstdc++ automatically.	



On 10/18/2010 10:30 PM, Michael Hertling wrote:
> On 10/19/2010 03:43 AM, Michael Scot Breitenfeld wrote:
>>       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.
> 
> Set the LINKER_LANGUAGE property of your main program target
> to "Fortran", and see the CMAKE_<LANG>_LINKER_PREFERENCE and
> CMAKE_<LANG>_LINKER_PREFERENCE_PROPAGATES variables for more
> information.
> 
> Regards,
> 
> Michael
> 
>> 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)
> _______________________________________________
> Powered by www.kitware.com
> 
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
> 
> Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
> 
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake



More information about the CMake mailing list