0

I am trying to build a CMake C++ Project with C++20 because I want to use the <ranges> library. I have tested my gcc compiler with the -std=c++20 flag and it works. However, when I build a project with CMake, it always chooses my (Apple) system's clang compiler, which can't find the <ranges> library.

This is despite me putting

set(CMAKE_CXX_STANDARD          20)  
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS        OFF)

in my CMakeLists.txt file. I even tried set(CMAKE_CXX_EXTENSIONS ON), as well as including the line set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20") (which I don't think is technically necessary but some stackoverflow post suggested it), and neither of them worked. How can I get CMake to automatically find the right compiler?

Paradox
  • 1,935
  • 1
  • 18
  • 31
  • 2
    https://stackoverflow.com/questions/45933732/how-to-specify-a-compiler-in-cmake ? `This is despite me putting` These variables specify options to the current compiler, they are not used to choose the compiler. – KamilCuk Dec 27 '20 at 21:49
  • 1
    That is an option, but how can I get CMake to automatically find the right compiler? (Common advice is to never manually set a compiler in CMake) – Paradox Dec 27 '20 at 21:50
  • 1
    Modify cmake sources and set a different list of compilers to set, inside [CMakeDetermineCCompiler.cmake](https://github.com/Kitware/CMake/blob/master/Modules/CMakeDetermineCCompiler.cmake#L62). Or remove "system's clang compiler" or replace it. It's unclear to me - "how" is a broad question, you have to "do something" anyway. How _exactly_ do you _want_ cmake to "automatically find the right compiler"? Which compiler is "right"? how to measure "right"ness of a compiler? You may want to go through list of compilers and check if they support c++20 - so go and write code for that. – KamilCuk Dec 27 '20 at 22:01
  • 1
    Thanks, I didn't catch the clarification to your first comment before, but I think that makes sense. I'll try what you suggested. – Paradox Dec 27 '20 at 22:06
  • 1
    The suggestion by @KamilCuk is the last thing to consider. `cmake` is a cross-platform tool and you should not expect the exact compiler or compiler flags to be part of decent `CMakeLists.txt` contents. I write from a friend's machine so I cannot test my recommendation or write a detailed, verified answer, but the way to go is this: set the compiler via environment variable or `ccmake` or edit `CMakeCache.txt`. I would prefer `ccmake .` inside the binary (a.k.a.` build`) directory. Even If you never used it, you''ll know what to do once you see the screen. The only natural way to go in cmake. – zkoza Dec 27 '20 at 22:20
  • CMake doesn't select a compiler according to variables like `CMAKE_CXX_STANDARD`. CMake firstly selects a compiler (according to your environment variables like `CXX` or CMake variables like `CMAKE_CXX_COMPILER`). Only then it tries to adjust flags for the selected compiler according to `CMAKE_CXX_STANDARD`. – Tsyvarev Dec 27 '20 at 23:56

0 Answers0