user615344
user615344

Reputation: 63

How to use CMake for Fortran and C++?

I am new to CMake and it seems really hard to get my script work. My codes can be compiled the usual way, but I really need to use CMake. I compiled with the following:

g++ vectc.cpp -c -std=c++11
gfortran vectf.f vectc.o -lstdc++

This CMakeLists.txt, that doesn't work for me:

cmake_minimum_required (VERSION 2.6)
project (add_vectors CXX Fortran)
enable_language(Fortran)
set(CMAKE_CXX_FLAGS "-c -std=c++11")
set(CMAKE_Fortran_FLAGS "CMakeFiles/executable/vectc.o -lstdc++")
add_executable( executable
vectc.cpp
vectf.f)

If I run make after cmake i get the following, and I really dont know what to do:

[ 33%] Linking CXX executable executable
c++: warning: CMakeFiles/executable.dir/vectf.f.o: linker input file unused because linking not done
c++: warning: CMakeFiles/executable.dir/vectc.cpp.o: linker input file unused because linking not done
[100%] Built target executable

Does anyone can help me with it?

Edit: The comments shows, I did not asked well. I am pretty new to the Cmake, and I don't know why I got the warnings. Also I have not found my "executable" file.

Upvotes: 2

Views: 4119

Answers (1)

vre
vre

Reputation: 6744

Reformulating my previous comment as an answer:

In case of mixed language sources (CXX, Fortran) the CXX linker is used by CMake because its linker preference is higher than that of the Fortran linker. But because of the PROGRAM statement in the fortran source the Fortran Linker is needed. Setting the LINKER_LANGUAGE property by set_property(TARGET executable PROPERTY LINKER_LANGUAGE Fortran) gives CMake a hint to select the correct linker.

Upvotes: 4

Related Questions