For starting a new project, see ModernCppStarter repo from original author of CPM.
Testing how to set up cmake for project with following structure:
.
├── CMakeLists.txt
├── include
│ ├── low
│ │ └── low.h
│ └── mid
│ └── mid.h
├── Makefile
├── README.md
├── src
│ ├── low
│ │ └── low.cpp
│ ├── main.cpp
│ └── mid
│ └── mid.cpp
└── tests
├── CMakeLists.txt
└── low
└── test_low.cpp
and following dependencies (A -> B
means A
depends on B
):
main -> mid -> low
Check commits one-by-one as they:
- start with not using CMake at all and building the thing using plain g++,
- use the simplest CMake
- hide the growing complexity to Makefile
- adds configure, build and test to GitHub pipeline.
- use hierarchical CMake files
- add unit tests
- adds AI based code review
- Use external dependencies
- Use CPM
- Linting
Without any build system help we are getting nasty g++
invocation like this:
mkdir build
g++ -I include -o build/hello src/main.cpp src/mid/mid.cpp src/low/low.cpp
You can run your program using
./build/hello
It works, but it is not nice and for more complicated project this would get much more ugly.
All configuration is set in the CMakeLists.txt
file.
Configure is required when modifying configuration, adding, renaming or moving files,
or when inter-files dependencies are changed and for the first run of coure.
It is done for make
build system by
cmake -S . -B build/
or e.g. to use ninja
cmake -S . -B build/ -G Ninja
The build itself then goes with:
cmake --build build/
You can run your program using
./build/hello
It started to get bit ugly in previous section so we added helper Makefile
to run previous commands. Now you can just use make run
This guide describes how to get debugger working. It is surprisingly neat, only thing to be aware of is that it does not binds to built-in Debug/Run (F5/Ctrl+F5) functionality but uses bottom bar buttons.