- 1. 📄 Description
- 2. 🛠️ Prerequisites
- 3. ✏️ Change project name
- 4. ⚖️ Change license
- 5. ⚙️ Generate project
- 6. 🏗️ Build project
- 7. 🏃♂️ Run project
- 8. 🏃♀️ Run tests
- 9. 📁 Adding new source files
- 10. 📝 Adding new tests
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.
Download and install CMake from the official website https://cmake.org/download/ and follow the instructions.
Download and install MSYS2 from the official website https://www.msys2.org/ and follow the instructions.
Open the MSYS2 terminal and run the following command:
pacman -S mingw-w64-x86_64-toolchainInside .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",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.
Change your name in the LICENSE file:
Copyright (c) 2024 <your_name>
For generating the project on your local windows machine, run the following command:
cmake . -B .\build\ -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=DebugIf you are using GitHub Codespaces, run the following command:
cmake . -B ./build/ -G "Ninja" -DCMAKE_BUILD_TYPE=Debugcmake --build .\build\.\build\bin\demo.exe.\build\bin\tests.exeAdd 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" <==
)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);
}