DrDirk
DrDirk

Reputation: 1997

Mixing C++ and Fortran

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.

Upvotes: 11

Views: 1321

Answers (1)

Pavel P
Pavel P

Reputation: 16940

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.

Upvotes: 4

Related Questions