π§° This repository is a reusable C++ + CMake starter template.
Click on "Use this template" (top right of the page) to start your own project based on it.
This repository provides a clean and cross-platform C++ project template using CMake and Makefile. It is designed for modern C++ development with easy compilation on Linux, Windows, and macOS, and supports both static and dynamic linking.
- π§± Minimal and modular
CMakeLists.txt
- π§ Customizable
Makefile
(Release / Debug / Clean) - π¦ Static linking support via
BUILD_STATIC
option - π οΈ Support for
Debug
andRelease
builds - π‘ Automatically generates
compile_commands.json
for LSPs - πͺπ₯οΈ Compatible with Windows, macOS, and Linux
- π§ͺ Ready for unit testing integration
project-root/
βββ CMakeLists.txt
βββ Makefile
βββ src/
β βββ main.cpp
make
make debug
make static
sudo make install
It will install the binaries in /usr/local/bin/
make test
make clean
./MyProject
You can run:
make help
to display all available build targets and customizable variables.
make
# or use CMake directly
cmake -B build -DCMAKE_BUILD_TYPE=Release -DBUILD_STATIC=ON
cmake --build build
Option | Default | Description |
---|---|---|
BUILD_STATIC |
OFF | Enables static linking of stdlib/gcc |
CMAKE_BUILD_TYPE |
Release | Enables optimization or debug symbols |
You can change the output directory of the final binary by modifying the following line in the Makefile
:
OUTPUT_DIR := . # Output directory for binaries (can be changed as needed)
This lets you control whether the binary appears in the project root, a bin/
folder, or elsewhere.
The CMake config includes a placeholder for adding tests:
enable_testing()
# add_subdirectory(test)
To adapt this template to your project:
-
Change the project name
Modify the following line inCMakeLists.txt
β―:project(MyProject VERSION 0.1.0 LANGUAGES CXX)
Replace
MyProject
with your project name. -
Add your source files
Place your.cpp
files insrc/
and your headers ininclude/
. CMakeLists automatically detects new files. -
Add dependencies Use
find_package()
oradd_subdirectory()
inCMakeLists.txt
to integrate external libraries. -
Disable Tests Set
-DBUILD_TESTS=OFF
to CMake or modify the option in the file. -
Change the Output Folder Modify the
OUTPUT_DIR
variable in theMakefile
.
To add an external dependency using FetchContent
, you can:
- Create a
.cmake
file for the library inside thecmake
folder. - Use
include(...)
in yourCMakeLists.txt
to load it. - Link it to your targets.
include(FetchContent)
FetchContent_Declare(
fmt
GIT_REPOSITORY https://github.com/fmtlib/fmt.git
GIT_TAG 11.2.0
)
FetchContent_MakeAvailable(fmt)
At the top of your CMakeLists.txt
, add:
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(fmt)
Then link fmt
to your target:
target_link_libraries(${PROJECT_NAME} PRIVATE fmt::fmt)
In the main.cpp
:
#include <fmt/core.h>
int main() {
fmt::print("Hello, formatted world!\n");
return 0;
}
This method keeps your project modular and clean, especially when managing multiple dependencies.
Make sure you have the following installed:
sudo apt install build-essential cmake
Or on macOS (with Homebrew):
brew install cmake
Install:
-
CMake
-
A C++ compiler like:
- MinGW (e.g. via MSYS2 or WinLibs)
- Visual Studio with Desktop C++ Tools
Make sure cmake
, make
(or ninja) are installed and your compiler are in the PATH.
This project is licensed under the MIT License β feel free to use it as a boilerplate for your own C++ projects.
Made with β€οΈ by Evr5 Feel free to contribute, fork, or suggest improvements!