1

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.

user615344
  • 53
  • 1
  • 5
  • 3
    What is wrong? Is the executable not built? I don't see any error. – Vladimir F Героям слава Jun 09 '17 at 12:41
  • Are you asking where the executable is? – Matthew Schuchard Jun 09 '17 at 15:00
  • You don't need `CMakeFiles/executable/vectc.o` in the `CMAKE_Fortran_FLAGS`, as you are specifying the dependency in the `add_executable` step. – Timothy Brown Jun 09 '17 at 15:21
  • Possible duplicate of [How to add "-l" (ell) compiler flag in CMake](https://stackoverflow.com/questions/43136418/how-to-add-l-ell-compiler-flag-in-cmake) – Tsyvarev Jun 09 '17 at 16:00
  • I am new to Cmake. I dont know why i got the `c++: warning: CMakeFiles/executable.dir/vectf.f.o: linker input file unused because linking not done` and the other warning, plus I cannot find the executable. – user615344 Jun 10 '17 at 09:01
  • Do any of the source contain an main (C/C++) or PROGRAM (Fortran) statement? Otherwise no linking can be done. – vre Jun 10 '17 at 09:39
  • Yes. Fortran has the Program statement and calls for a function from the C++code. The program works with the script mentioned earlier. – user615344 Jun 11 '17 at 06:14
  • It seems that the CXX linker is used but the Fortran Linker is needed. Try to set the LINKER_LANGUAGE property by `set_property(TARGET executable PROPERTY LINKER_LANGUAGE Fortran)`. – vre Jun 11 '17 at 10:31
  • Vre, please post your last comment as an answer, so I can mark my question solved. Thank you! – user615344 Jun 11 '17 at 11:35

1 Answers1

3

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.

vre
  • 6,041
  • 1
  • 25
  • 39