# cmake 3.28 and importing C++ headers in gcc

**URL:** https://discourse.cmake.org/t/cmake-3-28-and-importing-c-headers-in-gcc/9311
**Category:** Code
**Tags:** os:linux, comp:gcc
**Created:** [October 30, 2023, 3:52pm UTC](https://discourse.cmake.org/t/cmake-3-28-and-importing-c-headers-in-gcc/9311 "2023-10-30T15:52:36Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![John\_Fourkiotis](https://discourse.cmake.org/user_avatar/discourse.cmake.org/john_fourkiotis/32/3959_2.png) [@John\_Fourkiotis](https://discourse.cmake.org/u/John_Fourkiotis)
#### Post date: [October 30, 2023, 3:52pm UTC](https://discourse.cmake.org/t/cmake-3-28-and-importing-c-headers-in-gcc/9311/1 "2023-10-30T15:52:36Z")

</div>

Hi,

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

```make
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:**

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

```

**foo.cxx:**

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

```

**main.cxx:**

```cpp
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

---

<div class="post-metadata">

### Author: ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)
#### Post date: [October 30, 2023, 4:01pm UTC](https://discourse.cmake.org/t/cmake-3-28-and-importing-c-headers-in-gcc/9311/2 "2023-10-30T16:01:37Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![erk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/erk/32/114_2.png) [@erk](https://discourse.cmake.org/u/erk)
#### Post date: [October 30, 2023, 4:04pm UTC](https://discourse.cmake.org/t/cmake-3-28-and-importing-c-headers-in-gcc/9311/3 "2023-10-30T16:04:34Z")

</div>

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

---

<div class="post-metadata">

### Author: ![John\_Fourkiotis](https://discourse.cmake.org/user_avatar/discourse.cmake.org/john_fourkiotis/32/3959_2.png) [@John\_Fourkiotis](https://discourse.cmake.org/u/John_Fourkiotis)
#### Post date: [October 30, 2023, 4:50pm UTC](https://discourse.cmake.org/t/cmake-3-28-and-importing-c-headers-in-gcc/9311/4 "2023-10-30T16:50:49Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![John\_Fourkiotis](https://discourse.cmake.org/user_avatar/discourse.cmake.org/john_fourkiotis/32/3959_2.png) [@John\_Fourkiotis](https://discourse.cmake.org/u/John_Fourkiotis)
#### Post date: [October 30, 2023, 5:13pm UTC](https://discourse.cmake.org/t/cmake-3-28-and-importing-c-headers-in-gcc/9311/5 "2023-10-30T17:13:31Z")

</div>

Hi retif,

initially I used the following CMakeLists.txt:

```cmake
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/](https://www.kitware.com/import-cmake-the-experiment-is-over/). Then, I created a file mystd.cxx with the following contents:

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

```

and I added an import in main.cxx:

```cpp
import foo;
import mystd;
...

```

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

```sh
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

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [October 30, 2023, 5:18pm UTC](https://discourse.cmake.org/t/cmake-3-28-and-importing-c-headers-in-gcc/9311/6 "2023-10-30T17:18:18Z")

</div>

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