This educational repository contains a collection of examples showing how to use CMake for creating, building, installing, managing dependencies, and using libraries with CMake and C++. Additionally, the dependency chain between the libraries is represented by the following topics:
headerOnlyLibis a header-only libraryheaderOnlyLib/exampledemonstrate how to use theheaderOnlyLibsomeLibAis a static librarysomeLibA/exampledemonstrate how to use thesomeLibAsomeLibBis a static library, but it also depends onsomeLibAand therefore it's propagated whensomeLibBis consumed.someLibB/exampledemonstrate how to use thesomeLibBand its dependencysomeLibA.someLibCis a static library, but it depends onsomeLibB, which already has a dependecy on thesomeLibA.someLibC/exampledemonstrate how to use thesomeLibCand its dependencysomeLibBandheaderOnlyLib.Appdepends on thesomeLibA,someLibBandsomeLibC
NOTE: Tested on macOS/Ubuntu, this may not work on Windows
├── App # Application example
│ ├── CMakeLists.txt
│ └── src
├── README.md
├── LICENSE
├── headerOnlyLib # Header only library
│ ├── CMakeLists.txt
│ ├── cmake
│ ├── example
│ └── include
├── someLibA # Static library A
│ ├── CMakeLists.txt
│ ├── cmake
│ ├── example
│ ├── include
│ └── src
├── someLibB # Static library B which depends on A
│ ├── CMakeLists.txt
│ ├── cmake
│ ├── example
│ ├── include
│ └── src
└── someLibC # Static library C which depends on B and headerOnlyLib
├── CMakeLists.txt
├── cmake
├── example
├── include
└── src- CMake 3.20 or higher
- C++11 compatible compiler
-
Navigate to the library directory:
cd someLibA -
Create and navigate to the build directory:
mkdir -p build cd build -
Configure, build and install the library:
cmake .. cmake --build . --target install
-
Execute all the commands above with one command
make
-
Navigate to the
Appdirectory:cd App -
Create and navigate to the build directory:
mkdir -p build cd build -
Configure and build the application (make sure
someLibAandsomeLibBare installed and found by CMake):cmake .. cmake --build . ./app
You can find example usage of the library in the example folder.