Develop a C++ Application with Sibling Repositories
Sibling repositories keep an application and its libraries in separate Git repositories under one development directory. Each project keeps its own history and release cycle, while CMake builds the local checkouts together.
This layout is useful when a library is shared by several applications or must be versioned independently. It also means every developer must check out the required repositories before configuring the application.
Minimal example
The example contains one application and one static library:
In a real development directory, my_app and logging_lib would each be a
separate Git repository. The example keeps them together only so they can be
downloaded from this page.
The library
| logging_lib/include/logging/log.hpp | |
|---|---|
| logging_lib/src/log.cpp | |
|---|---|
| logging_lib/CMakeLists.txt | |
|---|---|
PUBLIC makes the library's include directory available to targets that link
logging_lib::logging_lib.
The application
| my_app/src/main.cpp | |
|---|---|
LOGGING_LIB_SOURCE is a CMake cache path supplied by the developer.
add_subdirectory() adds that source tree to the same build, and its second
argument gives the library a build directory outside its source repository.
Build and run
From the application repository, configure CMake with the path to the sibling library:
Expected output:
The path may be relative too. From the bundled example, use:
add_subdirectory() only combines the projects for this CMake build. It does
not combine their Git histories: changes to the application and library are
still committed in their respective repositories.
Use the sibling projects in VS Code
A multi-root workspace opens both repositories in one VS Code window. The workspace also passes the library path to the CMake Tools extension, so the graphical configure action is equivalent to the command-line example.
Place the workspace file beside the two repositories:
Then open it:
Install the recommended extensions when VS Code prompts, then run these commands from the Command Palette:
- CMake: Select a Kit — choose the compiler.
- CMake: Configure — configure
my_appand addlogging_libto its build. - CMake: Build — build the application and library.
- CMake: Run Without Debugging — run
my_app.
${workspaceFolder:logging_lib} resolves to the logging_lib folder named in
the workspace file. This avoids storing a developer-specific absolute path in
either Git repository.