Chocolate is a package manager for Windows
- Open Windows PowerShell as an Admin
- Run command:
Get-ExecutionPolicy
If you get "Restricted", then run:Set-ExecutionPolicy Bypass -Scope Process
orSet-ExecutionPolicy AllSigned
and accept by typing "yes" - Run command:
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
- Check by:
choco
Whole process should look like this
- Run command:
choco install mingw
- After installation run command:
refreshenv
or rerun PowerShell (remember that it should be run as admin) - Check that all is ok:
g++ --version
Whole process should look like this
- Run command:
choco install cmake --installargs 'ADD_CMAKE_TO_PATH=User'
- Reboot the system
After installation I got an error: "The term 'cmake' is not recognized" because of lack of environment variables. Reboot helps - Check that all is ok:
cmake --version
Whole process should look like this
- Download VSCode: https://code.visualstudio.com/download
- Install by downloaded installer
During installation set the point with environment variables - Reboot the system
It is possible to install VSCode by choco but default installer is good.
- Install extensions: cmake tools, С/С++
Optional extensions: git graph, git lens, back&forth
Click on extension panel, use search box, and press buttonInstall
- Setup cmake extension with MinGW
ClickCMake Tools/Manage/Extension Settings
- Scroll up to
Cmake: Generator
and enter:MinGW Makefiles
- Open folder (via VSCode) for a new project
For dialog box "Trust the authors" answer yes - Press
F1
. Entercmake
. ChooseCmake: Quick Start
- Choose MinGW compiler
- Input project name, e.g.
hello_world
Choose type of projectExecutable
Openmain.cpp
- For building press button
build
(look at the bottom) - For running press button
"Play"
(aroundBuild
button) - For debugging press
"Bug"
(aroundBuild
button) Breakpoint can be placed by clicking around line number
For step-by-step debugging use hot keys:F10, F11, F5
or panel on the top
The autogenerated CMakeLists.txt is good, but it can be enhanced.
Lets create src
folder and move main.cpp
there. Other *.cpp files also should be placed there.
Change the CMakeLists.txt:
cmake_minimum_required(VERSION 3.0.0)
project(my_project VERSION 0.1.0)
# program name
set(EXEC_NAME hello)
# set source folder
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/src SUB_SOURCES)
# add sources to compilation
add_executable(${EXEC_NAME} ${SUB_SOURCES})
# add paths to include folders. Usually it is "include" folder, not "src"
target_include_directories(${EXEC_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src)
# set the C++ standard
set_property(TARGET ${EXEC_NAME} PROPERTY CXX_STANDARD 17) #20
# set pedantic compiler which won't skip errors
#target_compile_options(${EXEC_NAME} PRIVATE -Wall -Wextra -Wpedantic -Werror)
# in case you need define
#target_compile_definitions(${EXEC_NAME} PRIVATE RUN_TESTS)
Sometimes you want to rebuild project from scratch. You can remove build
folder or run f1->cmake:clean, cmake:configure