error: C++26 was enabled in precompiled file 'CMakeFiles/__cmake_cxx23.dir/std.pcm' but is currently disabled

I see this strange error when I try to build with an installed CXX_MODULES,

The installed module was build with:

# CMake requires the language standard to be specified as compile feature
# when a target provides C++23 modules and the target will be installed
target_compile_features(Algo PUBLIC cxx_std_23)
bash-5.3$ make 
cmake --workflow --preset dev
Executing workflow step 1 of 4: configure preset "dev"

-- use ccache
-- CXXFLAGS=-stdlib=libc++
'brew' '--prefix' 'llvm'
-- LLVM_DIR=/usr/local/Cellar/llvm/21.1.8
-- CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES=/usr/local/Cellar/llvm/21.1.8/include/c++/v1;/usr/local/Cellar/llvm/21.1.8/lib/clang/21/include;/Library/Developer/CommandLineTools/SDKs/MacOSX14.sdk/usr/include
-- CMAKE_CXX_STDLIB_MODULES_JSON=/usr/local/Cellar/llvm/21.1.8/lib/c++/libc++.modules.json
-- CMAKE_CXX_COMPILER_IMPORT_STD=23;26
-- CMAKE_CXX_MODULE_STD=ON
-- ALGO_USE_MODULES=ON
CMake Warning (dev) at example/CMakeLists.txt:38 (add_test):
  uninitialized variable 'CMAKE_TOOLCHAIN_FILE'
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Configuring done (0.2s)
CMake Warning (dev) in CMakeLists.txt:
  CMake's support for `import std;` in C++23 and newer is experimental.  It
  is meant only for experimentation and feedback to CMake developers.
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Generating done (0.0s)
-- Build files have been written to: /Users/clausklein/Workspace/cpp/cxx20/cmake-init-modules/build/dev

Executing workflow step 2 of 4: build preset "dev"

[3/4] Install the project...
-- Install configuration: "Release"
-- Up-to-date: /Users/clausklein/Workspace/cpp/cxx20/cmake-init-modules/stagedir/lib/libAlgo.dylib
-- Up-to-date: /Users/clausklein/Workspace/cpp/cxx20/cmake-init-modules/stagedir/lib/cmake/my_package/modules/algo-interface.cppm
-- Up-to-date: /Users/clausklein/Workspace/cpp/cxx20/cmake-init-modules/stagedir/include/algo_export.h
-- Up-to-date: /Users/clausklein/Workspace/cpp/cxx20/cmake-init-modules/stagedir/lib/cmake/my_package/bmi-Clang_Release/algo.pcm
-- Up-to-date: /Users/clausklein/Workspace/cpp/cxx20/cmake-init-modules/stagedir/lib/cmake/my_package/my_package-targets.cmake
-- Up-to-date: /Users/clausklein/Workspace/cpp/cxx20/cmake-init-modules/stagedir/lib/cmake/my_package/my_package-targets-release.cmake
-- Up-to-date: /Users/clausklein/Workspace/cpp/cxx20/cmake-init-modules/stagedir/lib/cmake/my_package/./cxx-modules-my_package-targets.cmake
-- Up-to-date: /Users/clausklein/Workspace/cpp/cxx20/cmake-init-modules/stagedir/lib/cmake/my_package/./cxx-modules-my_package-targets-Release.cmake
-- Installing: /Users/clausklein/Workspace/cpp/cxx20/cmake-init-modules/stagedir/lib/cmake/my_package/./target-Algo-Release.cmake
-- Up-to-date: /Users/clausklein/Workspace/cpp/cxx20/cmake-init-modules/stagedir/lib/cmake/my_package/my_package-config-version.cmake
-- Up-to-date: /Users/clausklein/Workspace/cpp/cxx20/cmake-init-modules/stagedir/lib/cmake/my_package/my_package-config.cmake

Executing workflow step 3 of 4: test preset "dev"

Test project /Users/clausklein/Workspace/cpp/cxx20/cmake-init-modules/build/dev
    Start 3: find-package-test
    Start 1: app-tests
    Start 2: install-to-stagedir
1/3 Test #1: app-tests ........................   Passed    0.01 sec
2/3 Test #2: install-to-stagedir ..............   Passed    0.02 sec
3/3 Test #3: find-package-test ................***Failed    3.11 sec
Internal cmake changing into directory: /Users/clausklein/Workspace/cpp/cxx20/cmake-init-modules/build/dev/example/find-package-test
======== CMake output     ======
use ccache
CXXFLAGS=-stdlib=libc++
'brew' '--prefix' 'llvm'
LLVM_DIR=/usr/local/Cellar/llvm/21.1.8
CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES=/usr/local/Cellar/llvm/21.1.8/include/c++/v1;/usr/local/Cellar/llvm/21.1.8/lib/clang/21/include;/Library/Developer/CommandLineTools/SDKs/MacOSX14.sdk/usr/include
CMAKE_CXX_STDLIB_MODULES_JSON=/usr/local/Cellar/llvm/21.1.8/lib/c++/libc++.modules.json
CMAKE_CXX_COMPILER_IMPORT_STD=23;26
CMAKE_CXX_MODULE_STD=ON
ALGO_USE_MODULES=ON
Configuring done (0.2s)
CMake Warning (dev) in CMakeLists.txt:
  CMake's support for `import std;` in C++23 and newer is experimental.  It
  is meant only for experimentation and feedback to CMake developers.
This warning is for project developers.  Use -Wno-dev to suppress it.

Generating done (0.0s)
Build files have been written to: /Users/clausklein/Workspace/cpp/cxx20/cmake-init-modules/build/dev/example/find-package-test
======== End CMake output ======
Change Dir: '/Users/clausklein/Workspace/cpp/cxx20/cmake-init-modules/build/dev/example/find-package-test'

Run Clean Command: /Users/clausklein/.local/bin/ninja clean
[1/1] Cleaning all built files...
Cleaning... 26 files.

Run Build Command(s): /Users/clausklein/.local/bin/ninja
[1/15] Scanning /Users/clausklein/Workspace/cpp/cxx20/cmake-init-modules/build/dev/stagedir/lib/cmake/my_package/modules/algo-interface.cppm for CXX dependencies
warning: argument unused during compilation: '-c'
[2/15] Scanning /usr/local/Cellar/llvm/21.1.8/share/libc++/v1/std.compat.cppm for CXX dependencies
[3/15] Scanning /usr/local/Cellar/llvm/21.1.8/share/libc++/v1/std.compat.cppm for CXX dependencies
[4/15] Scanning /usr/local/Cellar/llvm/21.1.8/share/libc++/v1/std.cppm for CXX dependencies
[5/15] Scanning /usr/local/Cellar/llvm/21.1.8/share/libc++/v1/std.cppm for CXX dependencies
[6/15] Generating CXX dyndep file CMakeFiles/__cmake_cxx23.dir/CXX.dd
[7/15] Generating CXX dyndep file CMakeFiles/__cmake_cxx26.dir/CXX.dd
[8/15] Generating CXX dyndep file 'CMakeFiles/my_package__Algo@synth_e074a15ef2ea.dir/CXX.dd'
[9/15] Scanning /Users/clausklein/Workspace/cpp/cxx20/cmake-init-modules/example/main.cpp for CXX dependencies
[10/15] Generating CXX dyndep file CMakeFiles/app.dir/CXX.dd
[11/17] Building CXX object CMakeFiles/__cmake_cxx26.dir/usr/local/Cellar/llvm/21.1.8/share/libc++/v1/std.cppm.o
[12/17] Building CXX object CMakeFiles/__cmake_cxx23.dir/usr/local/Cellar/llvm/21.1.8/share/libc++/v1/std.cppm.o
[13/17] Building CXX object CMakeFiles/__cmake_cxx26.dir/usr/local/Cellar/llvm/21.1.8/share/libc++/v1/std.compat.cppm.o
[14/17] Building CXX object 'CMakeFiles/my_package__Algo@synth_e074a15ef2ea.dir/c42227a90345.bmi'
FAILED: [code=1] CMakeFiles/my_package__Algo@synth_e074a15ef2ea.dir/c42227a90345.bmi 
/usr/local/Cellar/llvm/21.1.8/bin/clang++ -DHAS_IMPORT_STD -I/usr/local/Cellar/llvm/21.1.8/include -I/usr/local/Cellar/llvm/21.1.8/include/c++/v1 -I/usr/local/Cellar/llvm/21.1.8/lib/clang/21/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX14.sdk/usr/include -isystem /Users/clausklein/Workspace/cpp/cxx20/cmake-init-modules/build/dev/stagedir/include -stdlib=libc++ -O3 -DNDEBUG -std=gnu++23 -fPIC -stdlib=libc++ --precompile -MD -MT 'CMakeFiles/my_package__Algo@synth_e074a15ef2ea.dir/c42227a90345.bmi' -MF CMakeFiles/my_package__Algo@synth_e074a15ef2ea.dir/c42227a90345.bmi.d @CMakeFiles/my_package__Algo@synth_e074a15ef2ea.dir/c42227a90345.bmi.modmap -fmodules-reduced-bmi -o 'CMakeFiles/my_package__Algo@synth_e074a15ef2ea.dir/c42227a90345.bmi' -c /Users/clausklein/Workspace/cpp/cxx20/cmake-init-modules/build/dev/stagedir/lib/cmake/my_package/modules/algo-interface.cppm
clang++: warning: argument unused during compilation: '-c' [-Wunused-command-line-argument]
error: C++26 was enabled in precompiled file 'CMakeFiles/__cmake_cxx23.dir/std.pcm' but is currently disabled
error: module file CMakeFiles/__cmake_cxx23.dir/std.pcm cannot be loaded due to a configuration mismatch with the current compilation [-Wmodule-file-config-mismatch]
/Users/clausklein/Workspace/cpp/cxx20/cmake-init-modules/build/dev/stagedir/lib/cmake/my_package/modules/algo-interface.cppm:17:17: error: use of undeclared identifier 'std'
   17 |   explicit Algo(std::string name)
      |                 ^
/Users/clausklein/Workspace/cpp/cxx20/cmake-init-modules/build/dev/stagedir/lib/cmake/my_package/modules/algo-interface.cppm:24:3: error: use of undeclared identifier 'std'
   24 |   std::string m_name;
      |   ^
/Users/clausklein/Workspace/cpp/cxx20/cmake-init-modules/build/dev/stagedir/lib/cmake/my_package/modules/algo-interface.cppm:18:16: error: use of undeclared identifier 'std'
   18 |       : m_name(std::move(name))
      |                ^
5 errors generated.
[15/17] Linking CXX static library lib__cmake_cxx26.a
ninja: build stopped: subcommand failed.



67% tests passed, 1 tests failed out of 3

Total Test time (real) =   3.11 sec

The following tests FAILED:
	  3 - find-package-test (Failed)
Errors while running CTest
make: *** [all] Error 8
bash-5.3$ 

I’m curious how C++26 got enabled for CMake::CXX23

But this is a known issue: we do not yet compile per-importer BMIs of any modules that are not imported at the moment. Work towards that is progressing.

It shouldn’t be possible with CMake-generated __CMAKE::CXX targets. It would be possible if a toolchain is providing the targets.

For the CMake generated targets the compile feature is constructed from the same string used to construct the name. It can’t gain other features, as this is done at generation time and unavailable for configuration to mess with.

auto const stdLevel =
  standardResolver.GetLevelString("CXX", *explicitLevel);
auto const cxxTargetName = cmStrCat("__CMAKE::CXX", stdLevel);

// ...

auto* stdlibTgt = makefile->AddImportedTarget(
  cxxTargetName, cmStateEnums::INTERFACE_LIBRARY, true);
cmCxxModuleMetadata::PopulateTarget(*stdlibTgt, *metadata, configs);
stdlibTgt->AppendProperty("IMPORTED_CXX_MODULES_COMPILE_FEATURES",
                          cmStrCat("cxx_std_", stdLevel));

The only exception is if a target with that name already exists, in which case CMake uses the user/toolchain provided target.

CXXFLAGS=-std=c++26 could do it, I suppose, but I feel like a small repro case so that we can reproduce it is needed here. In any case, whatever is getting C++26 enabled is the problem, but the root cause is not visible from the submitted information.

Could a full reproducer case please be provided?

Yes, sure:

you see the errors also on CI with GCC release builds and c++26