# Share source folder with other cmake projects

**URL:** <https://discourse.cmake.org/t/share-source-folder-with-other-cmake-projects/2499>\
**Category:** Code\
**Created:** [January 9, 2021, 11:42am UTC](https://discourse.cmake.org/t/share-source-folder-with-other-cmake-projects/2499 "2021-01-09T11:42:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bugblatterbeast](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ec9cab/32.png) [@bugblatterbeast](https://discourse.cmake.org/u/bugblatterbeast)\
**Post date:** [January 9, 2021, 11:42am UTC](https://discourse.cmake.org/t/share-source-folder-with-other-cmake-projects/2499/1 "2021-01-09T11:42:36Z")

</div>

Hi,

how can I set up a source folder that is shared between different cmake projects?

I’d like to have a folder structure like that inside my repository

- Main application (CMake project)
- Data viewer tool (CMake project)
- Data import library for other applications (CMake project)
- Data structure (shared source)
- Special functions (shared source)

We’ve had it that way before we started to use CMake and of course I have to admit that it is not best practice to blow up a repository like that, but it was working perfectly.

When we started to use CMake, we didn’t see a way around splitting the repository up and turn the shared folders into static libraries. However, I think our development state is not ready for that yet. There’s still too much debugging going on and those static libraries in extra repositories checked out automatically by CMake are making it unnecessarily complicated. I do think that it’s a totally neat feature and I’d like to have it that way once we publish. But right now it’s slowing us down way too much and I’d really like to have my single repository with shared source folders back.

How can I set up a source folder without making it exclusive to one CMake project?

```
target_sources(
    PROJECT_NAME PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/source1.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/source1.h
    ${CMAKE_CURRENT_SOURCE_DIR}/source2.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/source2.h
    ${CMAKE_CURRENT_SOURCE_DIR}/source3.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/source3.h
    )

```

I’ve tried to replace PROJECT\_NAME with variable ${CMAKE\_PROJECT\_NAME}, but that didn’t work.

Best regards

edit: Of course I played around with PRIVATE keyword as well still I didn’t find the right combination. Maybe I was doing it wrong.

edit2: I’m beginning to think that code section would be a better place for this question. Sorry for the confusion.

---

<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:** [January 11, 2021, 8:06pm UTC](https://discourse.cmake.org/t/share-source-folder-with-other-cmake-projects/2499/2 "2021-01-11T20:06:24Z")

</div>

As with many things at this level, there’s no right answer. Some details that would help me at least:

- Are all of these projects in one repository, multiple repos combined with submodules (or equivalent)?
- Are they intended to be built independently as well?
- Are you building a product or a project (Product being something without an API that someone else would `find_package` on.)?

Cc: @craig.scott

---

<div class="post-metadata">

**Author:** ![bugblatterbeast](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ec9cab/32.png) [@bugblatterbeast](https://discourse.cmake.org/u/bugblatterbeast)\
**Post date:** [January 11, 2021, 8:35pm UTC](https://discourse.cmake.org/t/share-source-folder-with-other-cmake-projects/2499/3 "2021-01-11T20:35:39Z")

</div>

I think maybe I’ve even added to many details by describing my motivation and stuff.

- The goal I have in mind is to have all source code in one repository (at least in current development phase)
- Preferably the different binaries should build independently as well. But it would also be fine if they didn’t
- I guess we are building a project

Let me try to improve the explanation of what I have in mind. To put it in the simplest way it would look like this.

```
Project-Repository
  Directory containing source code only used by application A
  Directory containing source code only used by application B
  Directory containing source code that both applications use
```

---

<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:** [January 12, 2021, 5:11pm UTC](https://discourse.cmake.org/t/share-source-folder-with-other-cmake-projects/2499/4 "2021-01-12T17:11:32Z")

</div>

Yeah, this sounds fine. You can place code into libraries and executables however you want. Most simply, each directory’s `CMakeLists.txt` with an `add_library` or `add_executable` call with the list of sources for that component. You can use `target_link_libraries` to link code into executables as needed.

The [CMake Tutorial](https://cmake.org/cmake/help/latest/guide/tutorial/index.html) is a good place to start.
