Cannot build modules using cmake and clang-18: "compiler does not provide a way to discover the import graph dependencies"

Since the support is now stable (https://www.kitware.com/import-cmake-the-experiment-is-over/), I tried out C++ modules using cmake and clang 18.

However, compiling the source gave me:

The target named "<target>" has C++ sources that may use modules, but the
compiler does not provide a way to discover the import graph dependencies.
See the cmake-cxxmodules(7) manual for details.  Use the
CMAKE_CXX_SCAN_FOR_MODULES variable to enable or disable scanning.

clang --version:

Debian clang version 18.0.0 (++20240101112328+703e83611cd8-1~exp1~20240101112345.435)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

cmake --version:

cmake version 3.28.1    
CMake suite maintained and supported by Kitware (kitware.com/cmake).

CMakeLists.txt:

add_library(lib STATIC)
target_sources(lib
  PUBLIC
  FILE_SET CXX_MODULES FILES
  file.ccm
)

Any pointers?

Do you have a clang-scan-deps binary around?

In fact I did not, but even if I install it, that makes no difference.
I have a debian installation running, I installed cmake via the script from the download section and clang-18 from the official LLVM repo.

$ clang-scan-deps --version
clang-scan-deps
Debian LLVM version 18.0.0
  Optimized build.

But thanks for the effort.

Hmm. That is odd. Debian stable or testing? I’ll try it out in a container here.

1 Like

standard debian from WSL

$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux 12 (bookworm)
Release:        12
Codename:       bookworm
$ uname -a
Linux NAME 5.15.133.1-microsoft-standard-WSL2 #1 SMP Thu Oct 5 21:02:42 UTC 2023 x86_64 GNU/Linux
$ ls -lsa /usr/bin/ | grep clang
0 lrwxrwxrwx  1 root root          24 Jul 27 13:38 clang -> ../lib/llvm-18/bin/clang
0 lrwxrwxrwx  1 root root          26 Jul 27 13:38 clang++ -> ../lib/llvm-18/bin/clang++
0 lrwxrwxrwx  1 root root          26 Feb 17  2023 clang++-14 -> ../lib/llvm-14/bin/clang++
0 lrwxrwxrwx  1 root root          24 Feb 17  2023 clang-14 -> ../lib/llvm-14/bin/clang
0 lrwxrwxrwx  1 root root          26 Jan  1 12:23 clang++-18 -> ../lib/llvm-18/bin/clang++
0 lrwxrwxrwx  1 root root          24 Jan  1 12:23 clang-18 -> ../lib/llvm-18/bin/clang
0 lrwxrwxrwx  1 root root          43 Jan  1 12:23 clang-apply-replacements-18 -> ../lib/llvm-18/bin/clang-apply-replacements
0 lrwxrwxrwx  1 root root          41 Jan  1 12:23 clang-change-namespace-18 -> ../lib/llvm-18/bin/clang-change-namespace
0 lrwxrwxrwx  1 root root          30 Jan  1 12:23 clang-check-18 -> ../lib/llvm-18/bin/clang-check
0 lrwxrwxrwx  1 root root          27 Jan  1 12:23 clang-cl-18 -> ../lib/llvm-18/bin/clang-cl
0 lrwxrwxrwx  1 root root          28 Feb 17  2023 clang-cpp-14 -> ../lib/llvm-14/bin/clang-cpp
0 lrwxrwxrwx  1 root root          28 Jan  1 12:23 clang-cpp-18 -> ../lib/llvm-18/bin/clang-cpp
0 lrwxrwxrwx  1 root root          25 Dec  8 09:59 clangd-17 -> ../lib/llvm-17/bin/clangd
0 lrwxrwxrwx  1 root root          28 Jan  1 12:23 clang-doc-18 -> ../lib/llvm-18/bin/clang-doc
0 lrwxrwxrwx  1 root root          39 Jan  1 12:23 clang-extdef-mapping-18 -> ../lib/llvm-18/bin/clang-extdef-mapping
0 lrwxrwxrwx  1 root root          15 Jul 27 13:38 clang-format -> clang-format-18
0 lrwxrwxrwx  1 root root          31 Jan  1 12:23 clang-format-18 -> ../lib/llvm-18/bin/clang-format
0 lrwxrwxrwx  1 root root          20 Jul 27 13:38 clang-format-diff -> clang-format-diff-18
8 -rwxr-xr-x  1 root root        6273 Jan  1 12:23 clang-format-diff-18
0 lrwxrwxrwx  1 root root          40 Jan  1 12:23 clang-include-cleaner-18 -> ../lib/llvm-18/bin/clang-include-cleaner
0 lrwxrwxrwx  1 root root          38 Jan  1 12:23 clang-include-fixer-18 -> ../lib/llvm-18/bin/clang-include-fixer
0 lrwxrwxrwx  1 root root          39 Jan  1 12:23 clang-linker-wrapper-18 -> ../lib/llvm-18/bin/clang-linker-wrapper
0 lrwxrwxrwx  1 root root          29 Jan  1 12:23 clang-move-18 -> ../lib/llvm-18/bin/clang-move
0 lrwxrwxrwx  1 root root          40 Jan  1 12:23 clang-offload-bundler-18 -> ../lib/llvm-18/bin/clang-offload-bundler
0 lrwxrwxrwx  1 root root          41 Jan  1 12:23 clang-offload-packager-18 -> ../lib/llvm-18/bin/clang-offload-packager
0 lrwxrwxrwx  1 root root          31 Jan  1 12:23 clang-pseudo-18 -> ../lib/llvm-18/bin/clang-pseudo
0 lrwxrwxrwx  1 root root          30 Jan  1 12:23 clang-query-18 -> ../lib/llvm-18/bin/clang-query
0 lrwxrwxrwx  1 root root          33 Jan  1 12:23 clang-refactor-18 -> ../lib/llvm-18/bin/clang-refactor
0 lrwxrwxrwx  1 root root          31 Jan  1 12:23 clang-rename-18 -> ../lib/llvm-18/bin/clang-rename
0 lrwxrwxrwx  1 root root          39 Jan  1 12:23 clang-reorder-fields-18 -> ../lib/llvm-18/bin/clang-reorder-fields
0 lrwxrwxrwx  1 root root          29 Jan  1 12:23 clang-repl-18 -> ../lib/llvm-18/bin/clang-repl
0 lrwxrwxrwx  1 root root          27 Jan  2 14:34 clang-scan-deps -> /usr/bin/clang-scan-deps-18
0 lrwxrwxrwx  1 root root          34 Jan  1 12:23 clang-scan-deps-18 -> ../lib/llvm-18/bin/clang-scan-deps
0 lrwxrwxrwx  1 root root          31 Jan  1 12:23 clang-tblgen-18 -> ../lib/llvm-18/bin/clang-tblgen
0 lrwxrwxrwx  1 root root          19 Jul 27 13:38 git-clang-format -> git-clang-format-18
0 lrwxrwxrwx  1 root root          35 Jan  1 12:23 git-clang-format-18 -> ../lib/llvm-18/bin/git-clang-format
0 lrwxrwxrwx  1 root root          48 Jan  1 12:23 hwasan_symbolize-18 -> ../lib/llvm-18/lib/clang/18/bin/hwasan_symbolize
0 lrwxrwxrwx  1 root root          43 Jan  1 12:23 scan-build-18 -> ../share/clang/scan-build-18/bin/scan-build
0 lrwxrwxrwx  1 root root          41 Jan  1 12:23 scan-view-18 -> ../share/clang/scan-view-18/bin/scan-view

complete minimal example(same environment):

cmake_minimum_required(VERSION 3.28)
project(PACtrl LANGUAGES CXX)

add_library(test STATIC)
target_sources(test
  PUBLIC
  FILE_SET CXX_MODULES FILES
  test.ccm
)
set_property(TARGET test PROPERTY CXX_STANDARD 20)
set_property(TARGET test PROPERTY CXX_STANDARD_REQUIRED ON)
set_property(TARGET test PROPERTY CXX_EXTENSIONS OFF)
module;

export module test;

namespace test {
    int i(void)
    {
        return 1;
    }
}
$ cmake . build -GNinja
CMake Warning:
  Ignoring extra path from command line:

   "build"


-- Configuring done (0.0s)
CMake Error in CMakeLists.txt:
  The target named "test" has C++ sources that may use modules, but the
  compiler does not provide a way to discover the import graph dependencies.
  See the cmake-cxxmodules(7) manual for details.  Use the
  CMAKE_CXX_SCAN_FOR_MODULES variable to enable or disable scanning.


-- Generating done (0.0s)
CMake Generate step failed.  Build files cannot be regenerated correctly.

CMake’s test suite ran fine:

$ apt install clang-tools-18
$ CC=clang-18 CXX=clang++-18 ../cmake-3.28.1-linux-x86_64/bin/cmake -GNinja ../src -DCMake_TEST_HOST_CMAKE=1 -DCMake_TEST_MODULE_COMPILATION=named,compile_commands,collation,partitions,internal_partitions,export_bmi,install_bmi,shared,bmionly
$ ninja
$ CC=clang-18 CXX=clang++-18 ../cmake-3.28.1-linux-x86_64/bin/ctest -R CXXModules

Are you sure CMake is trying to use Clang and not GCC here?

Thanks, that was the issue. Somehow, GCC and clang got messed up.

CMake defaults to searching for cc and c++ which are (generally) GCC on Linux distros. If that doesn’t exist, GCC is searched for first.

1 Like