16

I would like some advises on the organization of a set of related but independent C++ projects stored in a single (git) repository. The projects use CMake.

For a simplified example we imagine 2 projects A and B, A depending on B. Most people developing A will get B via the packaging system. Thus they will compile only A. However, we should allow developers to compile both A and B themselves (and install it), separately or together.

Here is a proposal :

└── Repo1
    ├── CMakeLists.txt (1)
    ├── A
    │   ├── CMakeLists.txt (2)
    │   ├── include
    │   │   ├── aaa.h
    │   │   ├── aaaa.h
    │   │   └── CMakeLists.txt (3)
    │   └── src
    │       ├── aaa.cpp
    │       ├── aaaa.cpp
    │       └── CMakeLists.txt (4)
    ├── B
    │   ├── CMakeLists.txt (2)
    │   ├── include
    │   │   ├── bbb.h
    │   │   ├── bbbb.h
    │   │   └── CMakeLists.txt (3)
    │   └── src
    │       ├── bbb.cpp
    │       ├── bbbb.cpp
    │       └── CMakeLists.txt (4)
    └── test
        ├── CMakeLists.txt (5)
        └── testaaaa.cpp
  1. Define the common cmake variables for all projects (if any) and includes the subdirectories.
  2. Defines the project itself and the project's required cmake variables.
  3. Defines the headers to install and the ones required for compilation.
  4. Configures the library and binaries.
  5. Configures the test executables and test-cases.

As I understand it, each project should produce a XXXConfig.cmake file and install it in /usr/local/share/cmake. Writing these files seem quite complicated when reading the documentation of CMake.

What do you think ? Does the structure make sense ?

Do you happen to have a working example of such a set of projects ?

1
  • I think you might be overshooting here - we just introduced cmake for one application with one dependee library project and so far are happy with one CMakeLists.txt file per project: A/CMakeLists.txt (the app) includes B/CMakeLists.txt (the library) using add_subdirectory(...).
    – ssc
    Jul 14, 2015 at 6:39

1 Answer 1

15

After quite some reading and tests, I have made a basic demo C++ project demonstrating the use of CMake, CTest + boost.test, CPack and Doxygen and using more or less the organization I mentioned in my question.

The project shows how to make subproject dependencies, how to compile the whole repo or only a subproject, how to package, how to test and how to produce documentation.

See here : https://github.com/Barthelemy/CppProjectTemplate

1
  • Hi @Barth, did you managed to do the same with target_source_directories (more modern CMake)? I am struggling with a very similar situation, but the included directories of B are not propagated to A (due to target_source_directories). Dec 20, 2021 at 20:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.