# How to configure multiple cmake projects with CMakePresets.json?

**URL:** https://discourse.cmake.org/t/how-to-configure-multiple-cmake-projects-with-cmakepresets-json/12673
**Category:** Code
**Tags:** tool:cmake
**Created:** [October 3, 2024, 1:28pm UTC](https://discourse.cmake.org/t/how-to-configure-multiple-cmake-projects-with-cmakepresets-json/12673 "2024-10-03T13:28:26Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![agostini01](https://discourse.cmake.org/user_avatar/discourse.cmake.org/agostini01/32/2131_2.png) [@agostini01](https://discourse.cmake.org/u/agostini01)
#### Post date: [October 3, 2024, 1:28pm UTC](https://discourse.cmake.org/t/how-to-configure-multiple-cmake-projects-with-cmakepresets-json/12673/1 "2024-10-03T13:28:26Z")

</div>

I recently found out about CMakePresets.json.  
I am trying to enable it in my workspace that has two `CMakeLists.txt` based projects.

The structure of my workspace is:

```auto
├── myproject
│ └── CMakeLists.txt
├── external
│ └── external-project
│ └── CMakeLists.txt
└── CMakePresets.json

```

Note that `myproject` depends on installed `lib` and `include` files from `external-project`.

How to I configure different presets for these two projects? Is this a supported use of `CMakePresets.json`?

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [October 3, 2024, 9:20pm UTC](https://discourse.cmake.org/t/how-to-configure-multiple-cmake-projects-with-cmakepresets-json/12673/2 "2024-10-03T21:20:07Z")

</div>

> [@agostini01](#):
>
> How to I configure different presets for these two projects? Is this a supported use of `CMakePresets.json`?

No, a `CMakePresets.json` file can only live in the same directory as the top level `CMakeLists.txt` of a project. You could create a `CMakePresets.json` file in each of your `myproject` and `externa/external-project` directories, or you could add a `CMakeLists.txt` to the top of your directory structure and have that add one of the other two directories, but I suspect neither is really going to give you quite what you were aiming for.

---

<div class="post-metadata">

### Author: ![agostini01](https://discourse.cmake.org/user_avatar/discourse.cmake.org/agostini01/32/2131_2.png) [@agostini01](https://discourse.cmake.org/u/agostini01)
#### Post date: [October 9, 2024, 2:17pm UTC](https://discourse.cmake.org/t/how-to-configure-multiple-cmake-projects-with-cmakepresets-json/12673/3 "2024-10-09T14:17:01Z")

</div>

Thank you for the clarification. I really appreciate it.

If I create `CMakePresets.json` for each project, which seems reasonable, do I have to navigate into the project folder to leverage the presets?

Can I reference the `external-project` preset commands from the `my-project` preset and trigger compilation of `external-project` from `my-project`? Or preset “libraries” (through `"include": ...` are supposed to live within a CMakeLists.txt enabled project?

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [October 9, 2024, 8:21pm UTC](https://discourse.cmake.org/t/how-to-configure-multiple-cmake-projects-with-cmakepresets-json/12673/4 "2024-10-09T20:21:16Z")

</div>

If you want a top level `CMakePresets.json` to work, you’ll need a top level `CMakeLists.txt` as well. You would need to put the logic in that top level `CMakeLists.txt` to select between the projects in the subdirectories. You could have the top level `CMakePresets.json` set a CMake cache variable that you query in the top level `CMakeLists.txt` to work out which one to pull in. Your top level `CMakePresets.json` can include the subdirectories’ `CMakePresets.json` files, but I doubt that will be what you want. You would not be able to have any presets with the same names across the subdirectories, for example. Any paths in the presets would also likely be wrong, or at least, have to be specified in a way that doesn’t matter which directory is the top level of the build.

Take a look at the [ExternalProject](https://cmake.org/cmake/help/latest/module/ExternalProject.html) module. It sounds like that might be relevant to you, but it depends on what you’re trying to achieve. It might not fit with the way you want to do things, and it comes with its own advantages and disadvantages, but it is worth mentioning in the conversation.
