11

Is there a way to use the Clang compiler while mixing C++ and Fortran?

Until now I use cmake with

project(mixing CXX Fortran)

but this triggers the use of g++.

-- The CXX compiler identification is GNU 6.2.0

CMakeLists.txt of my project with Fortran mixing:

cmake_minimum_required(VERSION 3.7.0)
project(mixing CXX Fortran)

# SET UP ROOT https://root.cern.ch/how/integrate-root-my-project-cmake
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} /opt/local/libexec/root6/etc/root/cmake)
find_package(ROOT REQUIRED COMPONENTS MATH MINUIT2)
include(${ROOT_USE_FILE})

include_directories(Experiment Theory ${ROOT_INCLUDE_DIRS})

add_executable(mixing main.cpp)

target_link_libraries(mixing ${ROOT_LIBRARIES})

Not working, because g++ cannot use the needed Clang flag -stdlib=libc++ of the ROOT library.

francescalus
  • 30,576
  • 16
  • 61
  • 96
DrDirk
  • 1,937
  • 3
  • 25
  • 36
  • Can you show us how you are invoking cmake, specifically how you specified the toolchain? –  Jan 04 '17 at 16:02
  • @Frank I use the command `cmake ..` from my build folder. What do you mean by toolchain? – DrDirk Jan 04 '17 at 16:20
  • If you are using gfortran and g++ together (excluding clang++, a reasonable tactic) it would seem you should permit g++ to use -lstdc++. – tim18 Jan 04 '17 at 16:22
  • 2
    There seems to be an example in the question [here](http://stackoverflow.com/q/25493039) which seems to show how to force different compilers. – d_1999 Jan 04 '17 at 16:30
  • @d_1999 I forced cmake now to use clang++ via `-DCMAKE_CXX_COMPILER=clang++` and for now it seems to work fine. – DrDirk Jan 04 '17 at 16:43
  • @d_1999 are you planning to write a proper answer? There is a bounty, and it would be fair if you received it, as you pointed to the right direction. (Or, the owner of the answer in the linked question). – Rodrigo Rodrigues Aug 10 '18 at 22:14

1 Answers1

4

You can always override c/c++ compiler by changing CMAKE_<LANG>_COMPILER, where <LANG> in your case is C or CXX.

  1. You can set your CC and CXX environment variables to override defaults for CMAKE_C_COMPILER / CMAKE_CXX_COMPILER:

CC=clang CXX=clang++ cmake

  1. You can set these on command line when invoking cmake:

cmake -D CMAKE_C_COMPILER=clang -D CMAKE_CXX_COMPILER=clang++

  1. You can also set these directly in your cmake file:

set(CMAKE_C_COMPILER clang) set(CMAKE_CXX_COMPILER clang++)

make sure however that you do that at the very top of your cmake file before any project/enable_language directive is used.

Pavel P
  • 15,789
  • 11
  • 79
  • 128
  • We found CMake is mostly broken for C++ projects. It will use the C compiler for tests even after setting the project to C++. We have experienced all kinds of stupid breaks, from failed feature tests to failed links because of it. – jww Aug 16 '18 at 12:45
  • In the last 10 years I always didn't like cmake (even though I even pushed commits to cmake). Funny thing, right now our team is discussing switching to something else (cmake or ...) and I'm strongly against :) – Pavel P Aug 16 '18 at 18:18