cxxmodules: Setting CMAKE_CXX_STANDARD before enable_language(CXX)
CMake fails to find Clang C++ compiler with errors about scan deps with Clang on Linux. CMake < 3.28.0 works, but 3.28.0+ is broken including 3.29.2.
The break is triggered by the set(CMAKE_CXX_STANDARD 20)
or 23
before enable_language(CXX)
Broken
Linux (Ubuntu 24.04 or CentOS 9 Stream) using apt install clang
or dnf install clang
with:
- Clang 17.0.6 (CentOS 9 Stream)
- Clang 18.1.3 (Ubuntu 24.04)
AND
- CMake >= 3.28.0
Not broken
-
project(scan LANGUAGES CXX)
before settingCMAKE_CXX_STANDARD
- GCC 13.2 on Linux (Ubuntu 24.04)
- GCC 14.1.0-RC-20240503 on Linux (CentOS Stream 9)
- GCC 15.0 nightly on macOS
- on Windows or macOS with CMake 3.29.2 and Clang 18.1.4
CMakeLists.txt
cmake_minimum_required(VERSION 3.15...3.30)
project(scan LANGUAGES NONE)
message(STATUS "CMake ${CMAKE_VERSION} Generator ${CMAKE_GENERATOR}")
set(CMAKE_CXX_STANDARD 20)
# this line triggers the bug if set to 20 or 23
enable_language(CXX)
OR CMakeLists.txt
cmake_minimum_required(VERSION 3.15...3.30)
set(CMAKE_CXX_STANDARD 20)
project(scan LANGUAGES CXX)
On Linux (Ubuntu 24.04) with CMake 3.29.2 and Clang 18.1.3 I get:
CXX=clang cmake -Bbuildlcl --fresh
-- CMake 3.29.2 Generator Ninja
-- The CXX compiler identification is Clang 18.1.3
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - failed
-- Check for working CXX compiler: /usr/bin/clang
-- Check for working CXX compiler: /usr/bin/clang - broken
CMake Error at /snap/cmake/1381/share/cmake-3.29/Modules/CMakeTestCXXCompiler.cmake:60 (message):
The C++ compiler
"/usr/bin/clang"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: '/tmp/buildlcl/CMakeFiles/CMakeScratch/TryCompile-MO441I'
Run Build Command(s): /ninja-1.12.0/ninja -v cmTC_03362
[1/4] "CMAKE_CXX_COMPILER_CLANG_SCAN_DEPS-NOTFOUND" -format=p1689 -- /usr/bin/clang -std=gnu++20 -x c++ /tmp/buildlcl/CMakeFiles/CMakeScratch/TryCompile-MO441I/testCXXCompiler.cxx -c -o CMakeFiles/cmTC_03362.dir/testCXXCompiler.cxx.o -resource-dir "/usr/lib/llvm-18/lib/clang/18" -MT CMakeFiles/cmTC_03362.dir/testCXXCompiler.cxx.o.ddi -MD -MF CMakeFiles/cmTC_03362.dir/testCXXCompiler.cxx.o.ddi.d > CMakeFiles/cmTC_03362.dir/testCXXCompiler.cxx.o.ddi.tmp && mv CMakeFiles/cmTC_03362.dir/testCXXCompiler.cxx.o.ddi.tmp CMakeFiles/cmTC_03362.dir/testCXXCompiler.cxx.o.ddi
FAILED: CMakeFiles/cmTC_03362.dir/testCXXCompiler.cxx.o.ddi
"CMAKE_CXX_COMPILER_CLANG_SCAN_DEPS-NOTFOUND" -format=p1689 -- /usr/bin/clang -std=gnu++20 -x c++ /tmp/buildlcl/CMakeFiles/CMakeScratch/TryCompile-MO441I/testCXXCompiler.cxx -c -o CMakeFiles/cmTC_03362.dir/testCXXCompiler.cxx.o -resource-dir "/usr/lib/llvm-18/lib/clang/18" -MT CMakeFiles/cmTC_03362.dir/testCXXCompiler.cxx.o.ddi -MD -MF CMakeFiles/cmTC_03362.dir/testCXXCompiler.cxx.o.ddi.d > CMakeFiles/cmTC_03362.dir/testCXXCompiler.cxx.o.ddi.tmp && mv CMakeFiles/cmTC_03362.dir/testCXXCompiler.cxx.o.ddi.tmp CMakeFiles/cmTC_03362.dir/testCXXCompiler.cxx.o.ddi
/bin/sh: 1: CMAKE_CXX_COMPILER_CLANG_SCAN_DEPS-NOTFOUND: not found
ninja: build stopped: subcommand failed.
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:9 (enable_language)
-- Configuring incomplete, errors occurred!
Edited by scivision