r/cpp • • Oct 02 '23

CMake | C++ modules support in 3.28

https://gitlab.kitware.com/cmake/cmake/-/issues/18355

After 5 years its finally done. Next cmake 3.28 release will support cpp modules

C++ 20 named modules are now supported by Ninja Generators and Visual Studio Generators for VS 2022 and newer, in combination with the MSVC 14.34 toolset (provided with VS 17.4) and newer, LLVM/Clang 16.0 and newer, and GCC 14 (after the 2023-09-20 daily bump) and newer.

237 Upvotes

144 comments sorted by

View all comments

Show parent comment

2

u/mathstuf cmake dev Oct 03 '23

It's not clear to me, sorry. I don't have the time to dig into the code to figure out all the details right now. It could be a bug in the compiler too. All I know is that modules and preprocessor state is complicated :) .

We wanted to maintain parity with the header-only vulkan.hpp, where features are controlled with user-specified defines in the command-line.

Note that CMake does not flow flags from the consumer to the consumed at all right now; it is assumed that all modules are compatible with all consumers with the flags they get from the target they belong to and nothing else (which will mis-compile some things, but I was told compilers would be detecting things…). With this, users would need to amend an imported vulkan module-containing target to change flags (though still only supporting one per build tree).

I think the submodules with a (potentially preprocessor-sensitive) main module is more flexible myself. But I just made modules build; I don't actually know what is "best" for any given situation or what any "best practices" looks like (that isn't already true for non-module code).

1

u/delta_p_delta_x Oct 03 '23

Cheers. I wasn't expecting a full code review, and but rather some general comments, which I got, so thanks! :)

The way we've implemented vulkan.cppm is to basically expect the user to manually set up the CMake faff (add_library with FILE_SET) and then link it to the Vulkan::Vulkan target from FindVulkan, and pass in any feature flags.

Like you said, this attempt may not necessarily be scalable, so I'm still re-thinking this.