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:
headerOnlyLib
is a header-only libraryheaderOnlyLib/example
demonstrate how to use theheaderOnlyLib
someLibA
is a static librarysomeLibA/example
demonstrate how to use thesomeLibA
someLibB
is a static library, but it also depends onsomeLibA
and therefore it's propagated whensomeLibB
is consumed.someLibB/example
demonstrate how to use thesomeLibB
and its dependencysomeLibA
.someLibC
is a static library, but it depends onsomeLibB
, which already has a dependecy on thesomeLibA
.someLibC/example
demonstrate how to use thesomeLibC
and its dependencysomeLibB
andheaderOnlyLib
.App
depends on thesomeLibA
,someLibB
andsomeLibC
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
App
directory:cd App
-
Create and navigate to the build directory:
mkdir -p build cd build
-
Configure and build the application (make sure
someLibA
andsomeLibB
are installed and found by CMake):cmake .. cmake --build . ./app
You can find example usage of the library in the example
folder.