CMake >= 3.28.0 + GCC >= 14 + CMAKE_CXX_STANDARD >= 20 check_source_compile bug
CMake 3.28.0 introduced a bug with GCC >= 14 and CMAKE_CXX_STANDARD >= 20 on Windows. This is seen when building a target or on check_source_compiles()
.
This is present in CMake 3.28.0 through nightly. The problem code seems to be in Modules/Compiler/GNU-CXX.cmake commit 4f041095 or so. Tested on Windows with MinGW GCC 14.1.0.
Update: #25974 (comment 1520112) appears to be Windows GCC specific. Would this be a candidate for CMake internally fencing off Windows GCC from module scanning until GCC fixes itself?
A little quirk is the check_source_compiles()
doesn't fail if there's no include, but that would be a less common use case.
A workaround I've put into projects not using C++ modules is after project()
and before any targets or check_*
follows. This will need to be left in there for existing CMake 3.28.x and CMake 3.29.x in the wild.
set(CMAKE_CXX_SCAN_FOR_MODULES OFF)
MWE
CMakeLists.txt
cmake_minimum_required(VERSION 3.25...3.28)
project(scan LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
include(CheckSourceCompiles)
# if any #include at all, fails check
check_source_compiles(CXX "#include <version>
int main() { return 0; }" ok)
CMakeConfigureLog.yml
cmakeVariables:
CMAKE_CXX_FLAGS: ""
CMAKE_CXX_FLAGS_DEBUG: "-g"
CMAKE_EXE_LINKER_FLAGS: ""
buildResult:
variable: "ok"
cached: true
stdout: |
Change Dir: 'C:/temp/build/CMakeFiles/CMakeScratch/TryCompile-372hwp'
Run Build Command(s): C:/msys64/mingw64/bin/ninja.exe -v cmTC_9eef0
[1/4] C:\\msys64\\mingw64\\bin\\g++.exe -Dok -std=gnu++20 -E -x c++ C:/temp/build/CMakeFiles/CMakeScratch/TryCompile-372hwp/src.cxx -MT CMakeFiles\\cmTC_9eef0.dir\\src.cxx.obj.ddi -MD -MF CMakeFiles\\cmTC_9eef0.dir\\src.cxx.obj.ddi.d -fmodules-ts -fdeps-file=CMakeFiles\\cmTC_9eef0.dir\\src.cxx.obj.ddi -fdeps-target=CMakeFiles\\cmTC_9eef0.dir\\src.cxx.obj -fdeps-format=p1689r5 -o CMakeFiles\\cmTC_9eef0.dir\\src.cxx.obj.ddi.i
[2/4] C:\\msys64\\mingw64\\bin\\cmake.exe -E cmake_ninja_dyndep --tdi=CMakeFiles\\cmTC_9eef0.dir\\CXXDependInfo.json --lang=CXX --modmapfmt=gcc --dd=CMakeFiles/cmTC_9eef0.dir/CXX.dd @CMakeFiles/cmTC_9eef0.dir/CXX.dd.rsp
[3/4] C:\\msys64\\mingw64\\bin\\g++.exe -Dok -std=gnu++20 -fmodules-ts -fmodule-mapper=CMakeFiles\\cmTC_9eef0.dir\\src.cxx.obj.modmap -MD -fdeps-format=p1689r5 -x c++ -o CMakeFiles/cmTC_9eef0.dir/src.cxx.obj -c C:/temp/build/CMakeFiles/CMakeScratch/TryCompile-372hwp/src.cxx
FAILED: CMakeFiles/cmTC_9eef0.dir/src.cxx.obj
C:\\msys64\\mingw64\\bin\\g++.exe -Dok -std=gnu++20 -fmodules-ts -fmodule-mapper=CMakeFiles\\cmTC_9eef0.dir\\src.cxx.obj.modmap -MD -fdeps-format=p1689r5 -x c++ -o CMakeFiles/cmTC_9eef0.dir/src.cxx.obj -c C:/temp/build/CMakeFiles/CMakeScratch/TryCompile-372hwp/src.cxx
C:/temp/build/CMakeFiles/CMakeScratch/TryCompile-372hwp/src.cxx: error: failed reading mapper 'CMakeFiles\\cmTC_9eef0.dir\\src.cxx.obj.modmap'
ninja: build stopped: subcommand failed.
exitCode: 1