CXX_MODULES: FAILED: CMakeFiles/Algo.dir/algo-impl.cpp.o.ddi

I have an import std; example based on C++20 Modules, CMake, And Shared Libraries

It builds on CI windows (MS cl), ubuntu (gcc-15, gcc-16), but not with clang-20/21!

@ben.boeckel But on my local iMac i368 (OSX) it build with clang-20 and a patched cmake v4.0.3?

see clang++ -print-file-name=libc++.modules.json does not print the full path to the file · Issue #226050 · Homebrew/homebrew-core · GitHub

==> Upgrading cmake
  3.31.6 -> 4.0.3 
==> Pouring cmake--4.0.3.arm64_sequoia.bottle.tar.gz


Run export hostSystemName=Darwin
Darwin
Executing workflow step 1 of 4: configure preset "dev"

-- The CXX compiler identification is Clang 20.1.7
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /opt/homebrew/opt/llvm/bin/clang++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
'brew' '--prefix' 'llvm@20'
-- LLVM_ROOT=/opt/homebrew/Cellar/llvm/20.1.7
-- ALGO_USE_MODULES=TRUE
-- CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES=/opt/homebrew/Cellar/llvm/20.1.7/include/c++/v1;/opt/homebrew/Cellar/llvm/20.1.7/lib/clang/20/include;/Library/Developer/CommandLineTools/SDKs/MacOSX15.sdk/usr/include
-- Performing Test COMPILER_HAS_HIDDEN_VISIBILITY
-- Performing Test COMPILER_HAS_HIDDEN_VISIBILITY - Success
-- Performing Test COMPILER_HAS_HIDDEN_INLINE_VISIBILITY
-- Performing Test COMPILER_HAS_HIDDEN_INLINE_VISIBILITY - Success
-- Performing Test COMPILER_HAS_DEPRECATED_ATTR
-- Performing Test COMPILER_HAS_DEPRECATED_ATTR - Success
CMake Warning (dev) at example/CMakeLists.txt:34 (add_test):
  uninitialized variable 'CMAKE_TOOLCHAIN_FILE'
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Configuring done (2.8s)
CMake Warning:
-- Generating done (0.0s)
  Manually-specified variables were not used by the project:
-- Build files have been written to: /Users/runner/work/cmake-init-modules/cmake-init-modules/build/dev


    BUILD_MCSS_DOCS
Executing workflow step 2 of 4: build preset "dev"
    cmake-init-modules_DEVELOPER_MODE


[1/11] Building CXX object CMakeFiles/Algo_verify_interface_header_sets.dir/Algo_verify_interface_header_sets/algo_export.h.cxx.o

[2/11] Scanning /Users/runner/work/cmake-init-modules/cmake-init-modules/example/main.cpp for CXX dependencies
[3/11] Scanning /Users/runner/work/cmake-init-modules/cmake-init-modules/algo-interface.cppm for CXX dependencies
[4/11] Scanning /Users/runner/work/cmake-init-modules/cmake-init-modules/algo-impl.cpp for CXX dependencies
FAILED: CMakeFiles/Algo.dir/algo-impl.cpp.o.ddi 
"/opt/homebrew/Cellar/llvm/20.1.7/bin/clang-scan-deps" -format=p1689 -- /opt/homebrew/opt/llvm/bin/clang++ -DAlgo_EXPORTS -I/Users/runner/work/cmake-init-modules/cmake-init-modules/build/dev -isystem /opt/homebrew/Cellar/llvm/20.1.7/include -Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Wcast-qual -Wformat=2 -Wundef -Werror=float-equal -Wshadow -Wcast-align -Wunused -Wnull-dereference -Wdouble-promotion -Wimplicit-fallthrough -Wextra-semi -Woverloaded-virtual -Wnon-virtual-dtor -Wold-style-cast -g -std=gnu++23 -arch arm64 -fPIC -fvisibility=hidden -fvisibility-inlines-hidden -x c++ /Users/runner/work/cmake-init-modules/cmake-init-modules/algo-impl.cpp -c -o CMakeFiles/Algo.dir/algo-impl.cpp.o -resource-dir "/opt/homebrew/Cellar/llvm/20.1.7/lib/clang/20" -MT CMakeFiles/Algo.dir/algo-impl.cpp.o.ddi -MD -MF CMakeFiles/Algo.dir/algo-impl.cpp.o.ddi.d > CMakeFiles/Algo.dir/algo-impl.cpp.o.ddi.tmp && mv CMakeFiles/Algo.dir/algo-impl.cpp.o.ddi.tmp CMakeFiles/Algo.dir/algo-impl.cpp.o.ddi
Error while scanning dependencies for /Users/runner/work/cmake-init-modules/cmake-init-modules/algo-impl.cpp:
In file included from /Users/runner/work/cmake-init-modules/cmake-init-modules/algo-impl.cpp:3:
In file included from /opt/homebrew/opt/llvm/bin/../include/c++/v1/print:46:
In file included from /opt/homebrew/opt/llvm/bin/../include/c++/v1/format:202:
In file included from /opt/homebrew/opt/llvm/bin/../include/c++/v1/__format/container_adaptor.h:20:
In file included from /opt/homebrew/opt/llvm/bin/../include/c++/v1/__format/range_default_formatter.h:23:
In file included from /opt/homebrew/opt/llvm/bin/../include/c++/v1/__format/range_formatter.h:23:
In file included from /opt/homebrew/opt/llvm/bin/../include/c++/v1/__format/format_context.h:28:
In file included from /opt/homebrew/opt/llvm/bin/../include/c++/v1/__locale:17:
In file included from /opt/homebrew/opt/llvm/bin/../include/c++/v1/__locale_dir/locale_base_api.h:117:
In file included from /opt/homebrew/opt/llvm/bin/../include/c++/v1/__locale_dir/support/apple.h:18:
/opt/homebrew/opt/llvm/bin/../include/c++/v1/__locale_dir/support/bsd_like.h:21:10: fatal error: 'time.h' file not found
ninja: build stopped: subcommand failed.
Error: Process completed with exit code 1.

The project is avail GitHub - ClausKlein/cmake-init-modules at feature/import-std-in-cmake-3-30

@craig.scott this is based on your example, but does not compile with clang++-20 on OSX CI?

algo-interface.cppm:

module;

#include <algo_export.h>  // <-- Generated header added to the global fragment

#ifdef HAS_IMPORT_STD
import std;
#else
#  include <string>  // for string
#  include <utility>  // for move
#endif

export module algo;  // <-- Annotation not currently required, but see discussion below

export class ALGO_EXPORT Algo  // <-- ALGO_EXPORT annotation added to the class definition
{
public:
  explicit Algo(std::string name)
      : m_name(std::move(name))
  {
  }
  void helloWorld();

private:
  std::string m_name;
};

algo-impl.cpp:

module;

#include <print>

module algo;

void Algo::helloWorld()
{
  std::print("hello {}\n", m_name);
}

I haven’t really been closely following the latest and greatest in C++20 modules. You probably need @ben.boeckel to comment on anything modules-related, especially with regard to using import std. My limited understanding is that this is still one area where compiler and tooling support is still very sketchy.

You might also want to provide details of the errors you get. Stating “does not compile” doesn’t give folks much to go on for why it fails.

with this patch it works on my iMac 386:
iMac:cmake clausklein$ git diff

diff --git a/Modules/Compiler/Clang-CXX-CXXImportStd.cmake b/Modules/Compiler/Clang-CXX-CXXImportStd.cmake
index 5330eb1a3a..7952193e84 100644
--- a/Modules/Compiler/Clang-CXX-CXXImportStd.cmake
+++ b/Modules/Compiler/Clang-CXX-CXXImportStd.cmake
@@ -14,7 +14,7 @@ function (_cmake_cxx_import_std std variable)
     COMMAND
       "${CMAKE_CXX_COMPILER}"
       ${CMAKE_CXX_COMPILER_ID_ARG1}
-      "-print-file-name=${_clang_modules_json_impl}.modules.json"
+      "-print-file-name=c++/${_clang_modules_json_impl}.modules.json"
     OUTPUT_VARIABLE _clang_libcxx_modules_json_file
     ERROR_VARIABLE _clang_libcxx_modules_json_file_err
     RESULT_VARIABLE _clang_libcxx_modules_json_file_res

iMac:cmake clausklein$ git describe --long --dirty
v4.1.0-rc1-0-g7e0e2e6aa2-dirty
iMac:cmake clausklein$