Reusing out of tree c++ module?


Waiting for the 3.28 official release, what would importing a module from another directory look like ?

The helloword example has foo.cxx in the same dir, but sooner or later we will end up with modules in some directories and project located elsewhere calling them.

Main proj dir
----- CMakeLists.txt
----- main.cpp ← import module in that source

Module dir
----- CMakeLists.txt
----- mod.cxx ← export module from that source
----- build/

A helloworld with that scenario would be nice :slightly_smiling_face:


1 Like

There are examples in the test suite. To get this to work, you’ll need:

  • a Ninja generator (Visual Studio doesn’t provide the needed information for CMake to do what is needed at build time);
  • a CXX_MODULES_DIRECTORY argument to the export and/or install(EXPORT) calls to generate the needed information;
  • consumption from a Ninja build (Visual Studio can support it, CMake just needs to do a bit more to get it all correct).

Once that is done, you just link the target as normal.

I missed the fact that modules are managed like static libs with a differing way to set the source.

With the examples it became clear.


The most straightforward way I could find to use multiple modules from out of tree, is to link the folders/files in the current project’s tree.

Otherwise, the experience is very verbose. Not sure if it has some complications down the road, but it is the easiest way I could find right now.

This sounds like a hack and not something likely to work long-term or scale well. Why is using the imported targets not sufficient?

You are right, forgot to update, but I ended doing it the usual way, with specifying the other folders in the current CMake.

Thanks Ben,