From 452dd2b9f85811efc40a072959be189a2e673aee Mon Sep 17 00:00:00 2001 From: guoliang <648100535@qq.com> Date: Fri, 17 Oct 2025 21:57:01 +0800 Subject: [PATCH] [fix] error: module file CMakeFiles/__cmake_cxx23.dir/std.pcm cannot be loaded due to a configuration mismatch with the current compilation #4279 --- CMakeLists.txt | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3c055132..c070d3ca 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -81,6 +81,15 @@ function(add_module_library name) # `std` is affected by CMake options and may be higher than C++20. get_target_property(std ${name} CXX_STANDARD) + # Respect CMAKE_CXX_EXTENSIONS when forming the -std flag used to + # precompile modules. A mismatch between -std=gnu++ and -std=c++ + # can cause "configuration mismatch" errors when loading .pcm files. + if (CMAKE_CXX_EXTENSIONS) + set(std_flag "-std=gnu++${std}") + else() + set(std_flag "-std=c++${std}") + endif() + if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") set(pcms) foreach (src ${sources}) @@ -94,15 +103,15 @@ function(add_module_library name) # Use an absolute path to prevent target_link_libraries prepending -l # to it. set(pcms ${pcms} ${CMAKE_CURRENT_BINARY_DIR}/${pcm}) - add_custom_command( - OUTPUT ${pcm} - COMMAND ${CMAKE_CXX_COMPILER} - -std=c++${std} -x c++-module --precompile -c - -o ${pcm} ${CMAKE_CURRENT_SOURCE_DIR}/${src} - "-I$,;-I>" - # Required by the -I generator expression above. - COMMAND_EXPAND_LISTS - DEPENDS ${src}) + add_custom_command( + OUTPUT ${pcm} + COMMAND ${CMAKE_CXX_COMPILER} + ${std_flag} -x c++-module --precompile -c + -o ${pcm} ${CMAKE_CURRENT_SOURCE_DIR}/${src} + "-I$,;-I>" + # Required by the -I generator expression above. + COMMAND_EXPAND_LISTS + DEPENDS ${src}) endforeach () # Add .pcm files as sources to make sure they are built before the library. @@ -112,11 +121,12 @@ function(add_module_library name) set(obj ${pcm_we}.o) # Use an absolute path to prevent target_link_libraries prepending -l. set(sources ${sources} ${pcm} ${CMAKE_CURRENT_BINARY_DIR}/${obj}) - add_custom_command( - OUTPUT ${obj} - COMMAND ${CMAKE_CXX_COMPILER} $ - -c -o ${obj} ${pcm} - DEPENDS ${pcm}) + add_custom_command( + OUTPUT ${obj} + COMMAND ${CMAKE_CXX_COMPILER} ${std_flag} -c -o ${obj} ${pcm} + "-I$,;-I>" + COMMAND_EXPAND_LISTS + DEPENDS ${pcm}) endforeach () endif () target_sources(${name} PRIVATE ${sources})