Skip to content

Repository files navigation

Static Badge Version: 0.1.0 Static Badge

C++ project template using CMake

📑 Table of contents

1. 📄 Description

This is a simple C++ project template using CMake. It is a good starting point for new projects. It provides a step-by-step guide on how to generate, build, and run a C++ project using CMake inclusing any prerequisites needed.

2. 🛠️ Prerequisites

2.1 Install CMake

Download and install CMake from the official website https://cmake.org/download/ and follow the instructions.

2.2 Install MinGW using MSYS2

Download and install MSYS2 from the official website https://www.msys2.org/ and follow the instructions.

2.2.1 Install g++ toolchain

Open the MSYS2 terminal and run the following command:

pacman -S mingw-w64-x86_64-toolchain

2.2.2 Update compiler path

Inside .vscode/c_cpp_properties.json file, update the compilerPath property to the path where the g++.exe is located. For example:

"compilerPath": "D:/msys64/mingw64/bin/g++.exe",

3. ✏️ Change project name

Rename the cpp_template directory and the files inside it to YOUR_APP_NAME.

Change the following line in YOUR_APP_NAME/CMakeLists.txt file:

project(cpp_template VERSION 0.1.0 LANGUAGES CXX)

to

project(YOUR_APP_NAME VERSION 0.1.0 LANGUAGES CXX)

YOUR_APP_NAME will be the name of your executable.

You will also have to change the program propery in .vscode/launch.json file:

"program": "${workspaceRoot}/build/bin/cpp_template.exe",

to

"program": "${workspaceRoot}/build/bin/YOUR_APP_NAME.exe",

to be able to debug your executable.

4. ⚖️ Change license

Change your name in the LICENSE file:

Copyright (c) 2024 <your_name>

5. ⚙️ Generate project

For generating the project on your local windows machine, run the following command:

cmake . -B .\build\  -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Debug

If you are using GitHub Codespaces, run the following command:

cmake . -B ./build/  -G "Ninja" -DCMAKE_BUILD_TYPE=Debug

6. 🏗️ Build project

cmake --build .\build\

7. 🏃‍♂️ Run project

.\build\bin\demo.exe

8. 🏃‍♀️ Run tests

.\build\bin\tests.exe

8. 📁 Adding new source files

Add the following line in YOUR_APP_NAME/CMakeLists.txt: file:

target_sources(
  ${PROJECT_NAME}
  PUBLIC
    "${PROJECT_SOURCE_DIR}/${PROJECT_NAME}/YOUR_APP_NAME.hpp"
    "${PROJECT_SOURCE_DIR}/${PROJECT_NAME}/NEW_FILE.hpp" <==
  PRIVATE
    "${PROJECT_SOURCE_DIR}/${PROJECT_NAME}/YOUR_APP_NAME.cpp"
    "${PROJECT_SOURCE_DIR}/${PROJECT_NAME}/NEW_FILE.cpp" <==
)

9. 📝 Adding new tests

Add the following to tests/tests.cpp file:

// GIVEN: ...
// WHEN: ...
// THEN: ...
TEST(cpp_template_add, test_name) {
  EXPECT_EQ(something, something_else);
  EXPECT_NE(something, something_different);
}

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages