# C++ 20 Modules Update

**URL:** https://discourse.cmake.org/t/c-20-modules-update/7330
**Category:** Development
**Created:** [January 26, 2023, 8:20pm UTC](https://discourse.cmake.org/t/c-20-modules-update/7330 "2023-01-26T20:20:17Z")
**Posts on this page:** 12
**Page:** 6

<div class="post-metadata">

### Author: ![Przemek\_Koziol](https://discourse.cmake.org/user_avatar/discourse.cmake.org/przemek_koziol/32/3882_2.png) [@Przemek\_Koziol](https://discourse.cmake.org/u/Przemek_Koziol)
#### Post date: [September 28, 2023, 2:44pm UTC](https://discourse.cmake.org/t/c-20-modules-update/7330/104 "2023-09-28T14:44:19Z")

</div>

> <https://github.com/selassje/aoc22/blob/modules/imports/aoc22/day2.cppm>

Changing extension or adding -x c++ options solves this problem, but now I am getting linker errors:

[build] /usr/local/bin/g++ -DENABLE\_TESTS -I/home/pkoziol/repos/aoc22/src/…/include/aoc22 -isystem /home/pkoziol/repos/aoc22/tests/Catch2/src/catch2/… -isystem /home/pkoziol/repos/aoc22/builds/dev\_ninja\_gcc/tests/Catch2/generated-includes -g -std=c++20 -fmodules-ts -x c++ -Wall -Wextra -Wpedantic -Werror -Wshadow -Wnon-virtual-dtor -Wold-style-cast -Wcast-align -Wunused -Woverloaded-virtual -Wconversion -Wsign-conversion -Wmisleading-indentation -Wnull-dereference -Wdouble-promotion -Wformat=2 -Wimplicit-fallthrough -Wduplicated-cond -Wduplicated-branches -Wlogical-op -Wuseless-cast /home/pkoziol/repos/aoc22/src/day8.cpp -MT src/CMakeFiles/aoc22.dir/day8.cpp.o.ddi -MD -MF src/CMakeFiles/aoc22.dir/day8.cpp.o.ddi.d -fdeps-file=src/CMakeFiles/aoc22.dir/day8.cpp.o.ddi -fdeps-target=src/CMakeFiles/aoc22.dir/day8.cpp.o

[build] /usr/bin/ld: /lib/x86\_64-linux-gnu/crt1.o: in function `\_start’:

[build] (.text+0x1b): undefined reference to `main’

So two issues here:  
–the test targets are built first, before catch2 and the lib itself it seems  
–the main is somehow not being found

The non-module branch builds ok with the same gcc

---

<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: [September 28, 2023, 3:11pm UTC](https://discourse.cmake.org/t/c-20-modules-update/7330/105 "2023-09-28T15:11:53Z")

</div>

Where is `main` defined and how is it added to your target?

---

<div class="post-metadata">

### Author: ![Przemek\_Koziol](https://discourse.cmake.org/user_avatar/discourse.cmake.org/przemek_koziol/32/3882_2.png) [@Przemek\_Koziol](https://discourse.cmake.org/u/Przemek_Koziol)
#### Post date: [September 28, 2023, 3:21pm UTC](https://discourse.cmake.org/t/c-20-modules-update/7330/106 "2023-09-28T15:21:53Z")

</div>

> <https://github.com/selassje/aoc22/blob/modules/tests/tests_main.hpp>

Its included by every test executable.

---

<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: [September 28, 2023, 4:59pm UTC](https://discourse.cmake.org/t/c-20-modules-update/7330/107 "2023-09-28T16:59:52Z")

</div>

How is it included? And can you show the `add_library` or `target_sources` call which adds that source to the target?

---

<div class="post-metadata">

### Author: ![Przemek\_Koziol](https://discourse.cmake.org/user_avatar/discourse.cmake.org/przemek_koziol/32/3882_2.png) [@Przemek\_Koziol](https://discourse.cmake.org/u/Przemek_Koziol)
#### Post date: [September 28, 2023, 6:37pm UTC](https://discourse.cmake.org/t/c-20-modules-update/7330/108 "2023-09-28T18:37:29Z")

</div>

Example tests which includes it

> <https://github.com/selassje/aoc22/blob/modules/tests/day8.cpp>

The test executables are added here:

> <https://github.com/selassje/aoc22/blob/a9d16fcd0de7e9b0421c954f8abd2f6e914a74a6/tests/CMakeLists.txt#L16C1-L20C2>

add\_executable(${TEST\_NAME} ${TEST\_NAME}.cpp)

---

<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: [September 28, 2023, 6:40pm UTC](https://discourse.cmake.org/t/c-20-modules-update/7330/109 "2023-09-28T18:40:58Z")

</div>

The CMake looks OK. Can you diff the link lines with and without modules on the executables? Or the symbols in the objects. There has to be some difference…

Probably better to move this to an issue on the repo and bring the result back here instead of hijacking the topic.

---

<div class="post-metadata">

### Author: ![junglie85](https://discourse.cmake.org/user_avatar/discourse.cmake.org/junglie85/32/1734_2.png) [@junglie85](https://discourse.cmake.org/u/junglie85)
#### Post date: [November 9, 2023, 10:54am UTC](https://discourse.cmake.org/t/c-20-modules-update/7330/110 "2023-11-09T10:54:23Z")

</div>

Can you provide an example of how this should be specified please, because it’s not obvious to me.

Here’s my attempt:

```auto
target_sources(discovery_win32
        PUBLIC
        FILE_SET cxx_modules TYPE CXX_MODULES
        FILES
        "platform.win32.ixx" "platform.win32-window.ixx"
        PRIVATE
        "platform.win32-window.cpp")

```

```auto
C:\Dev\Personal\Discovery\out\build\x64-debug\Discovery\EXEC : CMake error : Output platform\win32\CMakeFiles\discovery_win32.dir\platform.win32-window.cpp.obj provides the `platform.win32:window` module but it is not found in a `FILE_SET` of type `CXX_MODULES`
  ninja: build stopped: subcommand failed

```

---

<div class="post-metadata">

### Author: ![junglie85](https://discourse.cmake.org/user_avatar/discourse.cmake.org/junglie85/32/1734_2.png) [@junglie85](https://discourse.cmake.org/u/junglie85)
#### Post date: [November 10, 2023, 2:14pm UTC](https://discourse.cmake.org/t/c-20-modules-update/7330/111 "2023-11-10T14:14:56Z")

</div>

I tried changing the name to be the project name (since custom targets are not allowed for `FILE_SET CXX_MODULES`, but that did not work either:

```auto
target_sources("${PROJECT_NAME}"
        PUBLIC
        FILE_SET CXX_MODULES FILES
        "platform.win32.ixx"
        "platform.win32-window.ixx"
        PRIVATE
        "platform.win32-window.cpp"
)

```

Subtle and perhaps not the most obvious. I appreciate this is very new, but perhaps a few examples would help. The [example from the CMake blog](https://www.kitware.com/import-cmake-the-experiment-is-over/) is a bit too basic.

```auto
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)

```

Could @ben.boeckel provide some clarification?

---

<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: [November 17, 2023, 3:42pm UTC](https://discourse.cmake.org/t/c-20-modules-update/7330/112 "2023-11-17T15:42:38Z")

</div>

I suspect that `platform.win32-window.cpp` has:

```cpp
module platform.win32:window;

```

at the top. If this is an implementation unit, it must be:

```cpp
module platform.win32;

```

regardless of what partition the declaration(s) of what it is implementing.

---

<div class="post-metadata">

### Author: ![junglie85](https://discourse.cmake.org/user_avatar/discourse.cmake.org/junglie85/32/1734_2.png) [@junglie85](https://discourse.cmake.org/u/junglie85)
#### Post date: [November 20, 2023, 2:52pm UTC](https://discourse.cmake.org/t/c-20-modules-update/7330/113 "2023-11-20T14:52:40Z")

</div>

That was exactly my problem. Thank you for helping me to understand how to split modules into multiple partitions.

---

<div class="post-metadata">

### Author: ![Risuwwv](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/r/8c91f0/32.png) [@Risuwwv](https://discourse.cmake.org/u/Risuwwv)
#### Post date: [December 14, 2023, 2:18am UTC](https://discourse.cmake.org/t/c-20-modules-update/7330/114 "2023-12-14T02:18:17Z")

</div>

Does that mean that any change to a module implementation unit cause the recompilation of the consumers of the module ?

EDIT: I was trying with gcc trunk and seeing this but with clang instead it seems to work.

---

<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: [December 19, 2023, 3:35am UTC](https://discourse.cmake.org/t/c-20-modules-update/7330/115 "2023-12-19T03:35:53Z")

</div>

> [@Risuwwv](#):
>
> Does that mean that any change to a module implementation unit cause the recompilation of the consumers of the module ?

Ideally, no. Consumers will need relinked as the object code has changed, but the implementation file should have no effect on BMI-producing files (just like a change to a `.cxx` shouldn’t make includers of the corresponding `.h` file care). If you see discrepancies, please file bugs with example code.

[Previous page](https://discourse.cmake.org/t/c-20-modules-update/7330.md?page=5)
