Share source folder with other cmake projects

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.

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

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

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 is a good place to start.