Skip to content

A cross-platform C++ project template using CMake and Makefile. Build easily on Linux, Windows, and macOS, with also static linking support.

License

Notifications You must be signed in to change notification settings

Evr5/cpp-make-cmake-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

22 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

C++ CMake Starter Template

🧰 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.

βœ… Features

  • 🧱 Minimal and modular CMakeLists.txt
  • πŸ”§ Customizable Makefile (Release / Debug / Clean)
  • πŸ“¦ Static linking support via BUILD_STATIC option
  • πŸ› οΈ Support for Debug and Release builds
  • πŸ’‘ Automatically generates compile_commands.json for LSPs
  • πŸͺŸπŸ–₯️ Compatible with Windows, macOS, and Linux
  • πŸ§ͺ Ready for unit testing integration

πŸ“ Project Structure

project-root/
β”œβ”€β”€ CMakeLists.txt
β”œβ”€β”€ Makefile
β”œβ”€β”€ src/
β”‚   └── main.cpp

πŸš€ Quick Start

πŸ–₯️ Linux / macOS

Release build

make

Debug build

make debug

Static builds

make static

Install binaries

sudo make install

It will install the binaries in /usr/local/bin/

Launch tests

make test

Clean builds

make clean

Run the program

./MyProject

Show available Makefile targets

You can run:

make help

to display all available build targets and customizable variables.

πŸͺŸ Windows (MinGW or WSL)

make
# or use CMake directly
cmake -B build -DCMAKE_BUILD_TYPE=Release -DBUILD_STATIC=ON
cmake --build build

βš™οΈ CMake Options

Option Default Description
BUILD_STATIC OFF Enables static linking of stdlib/gcc
CMAKE_BUILD_TYPE Release Enables optimization or debug symbols

πŸ› οΈ Output Directory

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.


πŸ§ͺ Testing (Optional)

The CMake config includes a placeholder for adding tests:

enable_testing()
# add_subdirectory(test)

πŸ§‘β€πŸ’» Use as a template

To adapt this template to your project:

  1. Change the project name
    Modify the following line in CMakeLists.txtβ€―:

    project(MyProject VERSION 0.1.0 LANGUAGES CXX)

    Replace MyProject with your project name.

  2. Add your source files
    Place your .cpp files in src/ and your headers in include/. CMakeLists automatically detects new files.

  3. Add dependencies Use find_package() or add_subdirectory() in CMakeLists.txt to integrate external libraries.

  4. Disable Tests Set -DBUILD_TESTS=OFF to CMake or modify the option in the file.

  5. Change the Output Folder Modify the OUTPUT_DIR variable in the Makefile.


πŸ“š Add external libraries with FetchContent (modular method)

To add an external dependency using FetchContent, you can:

  1. Create a .cmake file for the library inside the cmake folder.
  2. Use include(...) in your CMakeLists.txt to load it.
  3. Link it to your targets.

πŸ›  Example: Adding fmt

1. Create a new file at cmake/fmt.cmake

include(FetchContent)

FetchContent_Declare(
  fmt
  GIT_REPOSITORY https://github.com/fmtlib/fmt.git
  GIT_TAG        11.2.0
)

FetchContent_MakeAvailable(fmt)

2. Modify your CMakeLists.txt

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)

3. Use the library in your code

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.


🧰 Requirements

πŸ–₯️ Linux/macOS

Make sure you have the following installed:

sudo apt install build-essential cmake

Or on macOS (with Homebrew):

brew install cmake

πŸͺŸ Windows (with MinGW or VS)

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.


πŸ“„ License

This project is licensed under the MIT License β€” feel free to use it as a boilerplate for your own C++ projects.


✨ Author

Made with ❀️ by Evr5 Feel free to contribute, fork, or suggest improvements!

About

A cross-platform C++ project template using CMake and Makefile. Build easily on Linux, Windows, and macOS, with also static linking support.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published