Simple tutorial for CMake and C++.
Following this tutorial here: CMake Tutorial - CMake 3.25.1 Documentation
- Create a new folder and navigate inside:
my-cpp-project
- Create a new file called
main.cpp
and add a simple Hello World script:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
return 0;
}
- Create a new file called
CMakeLists.txt
:
# Set a minimum version of CMake
cmake_minimum_required(VERSION 3.10)
# Name your project (no spaces)
# Version is optional
project(MyCppProject VERSION 1.0)
# Create an executable for your project
add_executable(MyCppProject main.cpp)
- Create a
build
folder and navigate inside. - Generate a native build system for the project:
cmake ../
- This will default to whatever build system you have installed, like Visual Studio.
- You could also run
cmake ../ -A x64
and get a Visual Studio solution
- Now you can run the build:
cmake --build .
- You could also open the
.sln
file and runBuild Solution
for the same result.
- You could also open the
- Your
.exe
should be available inbuild\Debug\
- Ideally you should make a
.gitignore
and addbuild
to it.
Sets version to C++ 20:
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True)