cmake 3.28 and importing C++ headers in gcc

Hi,

I’m trying to create a CMakeLists.txt that more or less will do what the following Makefile does:

CXX=a path to gcc14 built from master 
MODULE_FLAGS = -fmodules-ts -flang-info-module-cmi

make_module_of_vector:
    $(CXX) -std=c++20 ${MODULE_FLAGS} -x c++-system-header vector

foo.o: foo.cxx
    $(CXX) -std=c++20 ${MODULE_FLAGS} -c foo.cxx

mystd.o: mystd.cxx
    $(CXX) -std=c++20 ${MODULE_FLAGS} -c mystd.cxx

all: main.cxx, mystd.o foo.o
    $(CXX) -std=c++20 ${MODULE_FLAGS} main.cxx foo.o mystd.o

The contents of each file are along the following lines:
mystd.cxx:

module;
export import <vector>;
export module mystd;

foo.cxx:

module;
#include <iostream>
export module foo;
// etc

main.cxx:

import foo;
import mystd;

int main() {
...
}

Although it is a toy example (and probably incorrect) it compiles and runs fine. I tried to achieve the same results by writing a CMakeLists.txt and use cmake 3.28, but I failed. Is this doable with cmake 3.28/gcc14 (master) ?

Thank you

It would be useful to know, what exactly has failed and with what error messages.
If you shared your current CMake project, it would be helpful too.

Did you read the recent blog post https://www.kitware.com/import-cmake-the-experiment-is-over/, about C++ module usage?

Hi Eric,

Thanks for taking an interest in my question. Yes I’ve read the post you mentioned and it mentions that only named modules are supported. I think it doesn’t support my use case although I may be wrong.

Hi retif,

initially I used the following CMakeLists.txt:

cmake_minimum_required(VERSION 3.28)
project(std_module_example CXX)

# Turning off extensions avoids and issue with the clang 16 compiler
# clang 17 and greater can avoid this setting
set(CMAKE_CXX_EXTENSIONS OFF)
# Set the version of C++ for the project
set(CMAKE_CXX_STANDARD 20)
# Create a library
add_library(foo)
# Add the module file to the library
target_sources(foo
  PUBLIC
    FILE_SET CXX_MODULES FILES
      foo.cxx
)
# Create an executable
add_executable(hello main.cxx)
# Link to the library foo
target_link_libraries(hello foo)

The contents are the same as in https://www.kitware.com/import-cmake-the-experiment-is-over/. Then, I created a file mystd.cxx with the following contents:

module;
export import <vector>
export module mystd;

and I added an import in main.cxx:

import foo;
import mystd;
...

I used the following commands to generate the appropriate vector.gcm and mystd.gcm:

g++ -std=c++20 -fmodules-ts -flang-info-module-cmi -x c++-system-header
g++ -std=c++20 -fmodules-ts -flang-info-module-cmi -c mystd.cxx

Then I updated the CMakeLists.txt to take into account mystd.o but when I run ninja I get the following error:

main.cxx:2:1:
mystd: error: failed to read compiled module: Unknown CMI mapping
mystd: note: imports must be built before being imported
mystd: fatal error: returning to the gate for a mechanical issue

Correct, there is zero support for importing header units at the moment.