16

CMake has experimental C++20 module dependency scanning (!5562). I try to use CMake 3.20, g++-11, and ninja-1.10 to build a project with module.

// main.cpp
import mod;
int main() { return 0; }
// mod.ixx
export module mod;
export void f() {}

The CMakeLists.txt is an adaptation of https://gitlab.kitware.com/ben.boeckel/cmake/blob/cpp-modules/Modules/Compiler/GNU-CXX.cmake

cmake_minimum_required(VERSION 3.20)
project(simple)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_DEPFILE_FORMAT gcc)
set(CMAKE_CXX_DEPENDS_USE_COMPILER TRUE)

string(CONCAT CMAKE_EXPERIMENTAL_CXX_SCANDEP_SOURCE
        "<CMAKE_CXX_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -E -x c++ <SOURCE>"
        " -MT <DYNDEP_FILE> -MD -MF <DEP_FILE>"
        " -fmodules-ts -fdep-file=<DYNDEP_FILE> -fdep-output=<OBJECT>"
        " -fdep-format=trtbd")

set(CMAKE_EXPERIMENTAL_CXX_MODULE_MAP_FORMAT "gcc")

set(CMAKE_EXPERIMENTAL_CXX_MODULE_MAP_FLAG
        " -fmodules-ts -fmodule-mapper=<MODULE_MAP_FILE>"
        " -fdep-format=trtbd -x c++")

set(CMAKE_CXX_FLAGS "-fmodules-ts")
add_executable(simple main.cpp mod.ixx)

But CMake and ninja don't build the project:

$ mkdir build && cd build
$ cmake -DCMAKE_CXX_COMPILER=<path to g++-11>/g++-11 -G Ninja ..

-- The C compiler identification is GNU 10.3.1
-- The CXX compiler identification is GNU 11.1.0
-- Detecting C compiler ABI info
...
-- Detecting CXX compile features - done
-- Configuring done 
-- Generating done
-- Build files have been written to: <...>

$ ninja

<...>/g++-11 -fmodules-ts -std=gnu++20 -MD -MT <...>/main.cpp.o 
    -MF <...>/main.cpp.o.d -o <...>/main.cpp.o -c ../main.cpp
In module imported at ../main.cpp:1:1:
mod: error: failed to read compiled module: No such file or directory
mod: note: compiled module file is ‘gcm.cache/mod.gcm’
mod: note: imports must be built before being imported
mod: fatal error: returning to the gate for a mechanical issue
compilation terminated.
ninja: build stopped: subcommand failed.

The top rated answer here (from ComicSansMS) says we can use the experimental feature to build C++20 modules: How to use c++20 modules with CMake?

Can CMake automatically scan module dependency and build the project?

Qurious Cube
  • 261
  • 2
  • 5

1 Answers1

1

GCC + CMake's module system is incredibly experimental at this point and, in my opinion, not worth tampering with right now. I spent about an hour trying to get it to automatically scan but I don't think the docs are complete yet to state how if at all possible. I'll need to look at the github issues for unofficial documentation to see if there is a way at all. As it stands, I don't see a way to do this.

Your best option is to work with Visual C++'s module system as their C++20 implementation is feature complete (sans final ABI implementation). I've worked with it and it was stable enough for my test projects aside from internal compilation errors. Those were due to complex constevals which are mitigated by simplifying them. I'm sure this same issue exists with other complex module code, so be aware to see if it fits your project goals.

j__
  • 632
  • 4
  • 18
  • 2
    Any update on this? – Henk Oct 20 '21 at 17:58
  • 1
    I don't quite get how MSVC"s C++20 implementation is feature complete, I've had some simple cases where the compiler would crash when trying to compile modules... – A. Smoliak Oct 29 '21 at 11:39
  • I am getting: `g++-11: error: unrecognized command-line option '-fdep-file=CMakeFiles/modules.dir/calc.cpp.o.ddi'` now using g++ 11.1.0. Apparently g++-11 is still not supporting this. See https://gitlab.kitware.com/cmake/cmake/-/issues/18355 – Zitrax Jun 14 '22 at 11:09