# C++20 module problems

**URL:** https://discourse.cmake.org/t/c-20-module-problems/10928
**Category:** Usage
**Created:** [June 1, 2024, 4:13pm UTC](https://discourse.cmake.org/t/c-20-module-problems/10928 "2024-06-01T16:13:18Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Moerten](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/a698b9/32.png) [@Moerten](https://discourse.cmake.org/u/Moerten)
#### Post date: [June 1, 2024, 4:13pm UTC](https://discourse.cmake.org/t/c-20-module-problems/10928/1 "2024-06-01T16:13:18Z")

</div>

Hi, now Fedora 40 was shipped with CMAKE \>= 3.28 I thought it’s time to use the C++20 modules feature. But I am having problems to use it.

First after some research I found out it is not allowed any more to use e.g.  
“-fmodules-ts” compiler switches for clang. This is because it would enable a non-standard modules implementation. Good to know.

Second I found out I had to modify my CMakeLists.txt like

```auto
target_sources(
	${CMAKE_PROJECT_NAME}
	PUBLIC
		${SOURCES}
		${HEADERS}
	PUBLIC
	FILE_SET CXX_MODULES
		FILES
			${MODULES}
)

```

Good to know also. With this I can use modules in my application and compile it. But now I am having several libraries this application is using. So I thought to use a module for testing purposes in one of the libs also. But there are two problems I am facing. I can compile the lib without problems but if it comes to compile the application depending on it I get the following error message:

“fatal error: module ‘XXXX’ not found”

Ok, so I thought maybe throwing all .pcm files in one folder “Modules” and provide the compiler switch

-fprebuilt-module-path="${PROJECT\_PATH}/Modules""

But this feels somehow wrong and surely is non standard c++ conform. It’s clang specific. So I thought there must be a standard way of doing so?

Also there is another problem related to this. I can specify the folder for the lookup of the pcm files. But how the hell one can tell CMAKE to but the pcm files to it? For now the pcm files are put in the “…/CMakeFiles/objlib.dir” folder.

And finally if the paths in the target\_sources FILES section containing “…” like “My/Path/…/…/to/the/file.ixx”. There appears an error:

```auto
CMake Error in CMakeLists.txt:
  Target "objlib" has source file

    /My/Path/../../../to/File.ixx

  in a "FILE_SET TYPE CXX_MODULES" but it is not scheduled for compilation.

```

Without the “…/…/” it compiles.

So how to avoid these problems? What is the CMAKE way to do it correctly? Or is it still in 2024 that C++ modules are broken?

---

<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: [June 3, 2024, 12:06pm UTC](https://discourse.cmake.org/t/c-20-module-problems/10928/2 "2024-06-03T12:06:58Z")

</div>

> [@Moerten](#):
>
> Without the “…/…/” it compiles.

This was fixed, I believe, in [this MR](https://gitlab.kitware.com/cmake/cmake/-/merge_requests/9286) which should be in 3.28.4; this patch version is not yet in Fedora it seems.

> [@Moerten](#):
>
> So I thought to use a module for testing purposes in one of the libs also. But there are two problems I am facing. I can compile the lib without problems but if it comes to compile the application depending on it I get the following error message:
> 
> “fatal error: module ‘XXXX’ not found”

Can you provide the full definition (values of variables, all related `target_*` commands, etc.) of your target and the application? This usually means that the module is not `PUBLIC` in the linked-to target or there is a missing `target_link_libraries()` between the targets.

---

<div class="post-metadata">

### Author: ![Moerten](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/a698b9/32.png) [@Moerten](https://discourse.cmake.org/u/Moerten)
#### Post date: [June 9, 2024, 8:23pm UTC](https://discourse.cmake.org/t/c-20-module-problems/10928/3 "2024-06-09T20:23:06Z")

</div>

> [@ben.boeckel](#):
>
> This was fixed, I believe, in [this MR](https://gitlab.kitware.com/cmake/cmake/-/merge_requests/9286) which should be in 3.28.4; this patch version is not yet in Fedora it seems.

Good to know it was fixed already. Hopefully Fedora will use the newer version soon.

> [@ben.boeckel](#):
>
> Can you provide the full definition (values of variables, all related `target_*` commands, etc.) of your target and the application? This usually means that the module is not `PUBLIC` in the linked-to target or there is a missing `target_link_libraries()` between the targets.

The Application’s CMakeLists.txt file looks like:

file(GLOB SOURCES “_.c" "_.cpp”)  
file(GLOB HEADERS “_.h" "_.hpp”)  
file(GLOB MODULES “_.m" "_.mpp” “_.cxx" "_.ixx” “\*.cppm”)

add\_executable(${CMAKE\_PROJECT\_NAME})

target\_sources(  
${CMAKE\_PROJECT\_NAME}  
PUBLIC  
${SOURCES}  
${HEADERS}  
PUBLIC  
FILE\_SET CXX\_MODULES  
FILES  
${MODULES}  
)

target\_compile\_features(${CMAKE\_PROJECT\_NAME} PUBLIC cxx\_std\_20)

target\_link\_libraries(  
${CMAKE\_PROJECT\_NAME}  
PUBLIC  
${MyLibA}  
…  
${MyLibN})

The lib’s CMakeLists.txt files look like:

file(GLOB\_RECURSE SOURCES “${PATH}/_.cpp")  
file(GLOB\_RECURSE HEADERS "${PATH}/_.hpp”)  
file(GLOB\_RECURSE MODULES “${PATH}/\*.ixx”)

add\_library(objlib OBJECT)

target\_sources(  
objlib  
PUBLIC  
${SOURCES}  
${HEADERS}  
PUBLIC  
FILE\_SET CXX\_MODULES  
BASE\_DIRS  
${SOURCE\_PATH}  
FILES  
${MODULES}  
)

target\_compile\_features(objlib PUBLIC cxx\_std\_20)

set\_property(TARGET objlib PROPERTY POSITION\_INDEPENDENT\_CODE 1)

add\_library(MyLib\_shared SHARED $\<TARGET\_OBJECTS:objlib\>)  
set\_target\_properties(MyLib\_shared PROPERTIES PREFIX “${OUTPUT\_NAME\_PREFIX}”)  
set\_target\_properties(MyLib\_shared PROPERTIES OUTPUT\_NAME “${CMAKE\_PROJECT\_NAME}${OUTPUT\_NAME\_SUFFIX}”)

add\_library(MyLib\_static STATIC $\<TARGET\_OBJECTS:objlib\>)  
set\_target\_properties(MyLib\_static PROPERTIES PREFIX “${OUTPUT\_NAME\_PREFIX}”)  
set\_target\_properties(MyLibA\_static PROPERTIES OUTPUT\_NAME “${CMAKE\_PROJECT\_NAME}${OUTPUT\_NAME\_SUFFIX}”)

But the libs are build separately from the application and I can’t think of a way the application could know where the pcm files are. My folder structure is like:

Project

- Bin
  - Debug
    - MyLibA.a
    - MyLibA.so
    - …
    - MyLibN.a
    - MyLibN.so

- Builds
  - MyApplication/Debug/CMakeFiles
  - MyLibA/Debug/CMakeFiles
  - …
  - MyLibN/Debug/CMakeFiles

- Sources
  - MyLibA
  - …
  - MyLibN

- Modules

Each lib and the application have their own workspace within the “Builds” folder. But the application knows only the “Sources” folders for the libs header files plus the “Bin” folder where the binaries are placed. But the application knows nothing about the workspaces of the libs inside the Builds folder where also the .pcm files are located.

So like I said before. If I copy the lib’s pcm files manually to e.g. the “Modules” folder and use the compiler argument…

-fprebuilt-module-path=“${PROJECT\_PATH}/Modules”"

…the application can see the pcm files. So how to handle these .pcm files with CMake?

---

<div class="post-metadata">

### Author: ![Moerten](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/a698b9/32.png) [@Moerten](https://discourse.cmake.org/u/Moerten)
#### Post date: [June 18, 2024, 7:29pm UTC](https://discourse.cmake.org/t/c-20-module-problems/10928/4 "2024-06-18T19:29:44Z")

</div>

I could solve the problem finally. I remembered to have read that these .pcm files are highly CPU spezfic and are not intended for exchange with a lib. But if these files are necessary to use the lib there was only one way that the application has to recreate these .pcm files (a second time) for itself. And this worked now.

But the next problem is coming. Now it seems includes in an .ixx file are not really visible for example for derived classes and thus one needs to add these includes again an again. But still investigating on this.

---

<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: [February 20, 2026, 3:50am UTC](https://discourse.cmake.org/t/c-20-module-problems/10928/5 "2026-02-20T03:50:45Z")

</div>

Correct; importing a module only makes what that module `export`s.
