4

Recently I've decided to "modularize" my library. No success. Here is the repo where you can find CMakeLists.txt & CMakePresets.json: kissra (GitHub)

The issue is that CMake cannot find standard std.cppm primary module interface - it is trying to find it in a wrong directory (/lib/share/libc++/v1) whereas the LLVM toolset is in fact installed in /usr/lib/llvm-21/. And I haven't figured out how to force CMake search PMIs in a different directory.

Here is the error I'm getting:

-- Configuring done (7.1s)
CMake Error at build/debug/CMakeFiles/4.2.1/CMakeCXXCompiler.cmake:111 (target_sources):
  Cannot find source file: /lib/share/libc++/v1/std.cppm
Call Stack (most recent call first):
  CMakeLists.txt:8 (project)

I've looked into the generated CMake scripts in the build directory and found this (CMakeCXXCompiler.cmake):

### Imported target for C++26 standard library
if (NOT TARGET "__CMAKE::CXX26")
if (NOT TARGET "__cmake_cxx26")
add_library(__cmake_cxx26 STATIC)
target_sources(__cmake_cxx26 INTERFACE "$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,STATIC_LIBRARY>:$<TARGET_OBJECTS:__cmake_cxx26>>")
set_property(TARGET __cmake_cxx26 PROPERTY EXCLUDE_FROM_ALL 1)
set_property(TARGET __cmake_cxx26 PROPERTY CXX_SCAN_FOR_MODULES 1)
set_property(TARGET __cmake_cxx26 PROPERTY CXX_MODULE_STD 0)
target_compile_features(__cmake_cxx26 PUBLIC cxx_std_26)
target_compile_options(__cmake_cxx26 PRIVATE -Wno-reserved-module-identifier)
target_include_directories(__cmake_cxx26 PRIVATE  "/lib/x86_64-linux-gnu/../share/libc++/v1")
target_sources(__cmake_cxx26
  PUBLIC
  FILE_SET std TYPE CXX_MODULES
    BASE_DIRS  "/lib/x86_64-linux-gnu/../share/libc++/v1"
    FILES  "/lib/x86_64-linux-gnu/../share/libc++/v1/std.cppm" "/lib/x86_64-linux-gnu/../share/libc++/v1/std.compat.cppm")
endif ()
add_library(__CMAKE::CXX26 ALIAS __cmake_cxx26)
endif ()
if (TARGET "__CMAKE::CXX26")
  list(APPEND CMAKE_CXX_COMPILER_IMPORT_STD "26")
endif ()

As you can see, CMake is trying to find PMIs in /lib/share/libc++/v1. Is there a way to customize the location for PMIs by setting some obscure global CMake variable in CMakePreset.json file?

1
  • I commend you on trying to take that step.The pioneers blaze the trail etc! ... myself, I'll probably wait another decade or so before I require C++23 as the minimum standard version for my library, at which time I'll be able to switch to importing the standard library modules. Anyway, +1 and made an edit. Commented Dec 22, 2025 at 13:24

1 Answer 1

2

CMAKE_CXX_STDLIB_MODULES_JSON may be helpful

OP update:
Here is a related LLVM issue: [clang] Wrong location for libc++.modules.json #172497
Basically, the issue is that clang returns an invalid path to libc++.modules.json file (CMake uses this file to locate the Standard Modules PMIs).

The fix is to explicitly specify the path to this libc++.modules.json file via CMAKE_CXX_STDLIB_MODULES_JSON CMake variable.
Example:
https://github.com/katkevich/kissra/blob/88f0f54a2b9b0a22a4d6a16816e71145cf13e8ba/CMakePresets.json#L15

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, thank you! Apparently, /usr/lib/llvm-21/bin/clang++ -print-file-name=libc++.modules.json returns invalid path. Here is an LLVM issue: https://github.com/llvm/llvm-project/issues/172497. Manually specifying CMAKE_CXX_STDLIB_MODULES_JSON does indeed help!
This answer must really be expanded. Right now it is just a link. What is that phrase? How would OP use it? Why is JSON relevant? etc. See also the FAQ item about using links and also the meta-SO post "Your answer is in another castle".
And if that's not enough - even following the link still doesn't quite tell us what OP should do and how that would work. So, I'm downvoting, but will happily un-downvote and maybe upvote when you edit.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.