# Reusing out of tree c++ module?

**URL:** https://discourse.cmake.org/t/reusing-out-of-tree-c-module/9332
**Category:** Usage
**Created:** [November 1, 2023, 7:48pm UTC](https://discourse.cmake.org/t/reusing-out-of-tree-c-module/9332 "2023-11-01T19:48:52Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Glad](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@Glad](https://discourse.cmake.org/u/Glad)
#### Post date: [November 1, 2023, 7:48pm UTC](https://discourse.cmake.org/t/reusing-out-of-tree-c-module/9332/1 "2023-11-01T19:48:52Z")

</div>

Hi,

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

The [helloword example](https://www.kitware.com/import-cmake-the-experiment-is-over) 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 🙂

Thanks

---

<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 3, 2023, 2:15am UTC](https://discourse.cmake.org/t/reusing-out-of-tree-c-module/9332/2 "2023-11-03T02:15:04Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Glad](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@Glad](https://discourse.cmake.org/u/Glad)
#### Post date: [November 4, 2023, 7:01am UTC](https://discourse.cmake.org/t/reusing-out-of-tree-c-module/9332/3 "2023-11-04T07:01:08Z")

</div>

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.

Thanks,

---

<div class="post-metadata">

### Author: ![Glad](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@Glad](https://discourse.cmake.org/u/Glad)
#### Post date: [November 4, 2023, 12:31pm UTC](https://discourse.cmake.org/t/reusing-out-of-tree-c-module/9332/4 "2023-11-04T12:31:10Z")

</div>

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.

---

<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, 1:29pm UTC](https://discourse.cmake.org/t/reusing-out-of-tree-c-module/9332/5 "2023-11-17T13:29:10Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Glad](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@Glad](https://discourse.cmake.org/u/Glad)
#### Post date: [November 17, 2023, 2:02pm UTC](https://discourse.cmake.org/t/reusing-out-of-tree-c-module/9332/6 "2023-11-17T14:02:03Z")

</div>

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,
