I know a lot of you are having troubles of getting it to work on Windows and complaining a shiton. I will admit, that most C++ programming books do NOT actually tell you how to set things up. And I think that's hilarious, because how are you supposed to program without a working development environment? Although some books do mention it, they usually target Unix operating systems.
You can just use Visual Studio, which is the best and beginner-friendly solution and has really good documentation from Microsoft, but for some reason you are just a boomer and don't want to use it and you are dumb to set up the alternatives.
Now, you have found the right guide! This guide aims to provide the fullest list of every possible main-stream IDEs/text editors you might want to use and its respective configuration on Windows.
Follow the guide and screenshot carefully. The screenshot are from Windows Sandbox, which is a clean install of Windows 10. If you followed everything, and can't get it work, open an issue. Let me see how that's even possible!!
A visual summary of this guide:
flowchart TD
A[Start]
A-->Compiler{Compiler}
A-->VisualStudio
Compiler-->MSYS2{MSYS2}
MSYS2--->GCC
MSYS2--->Clang
Compiler-->MSVC
GCC-->CMake
Clang-->CMake
MSVC-->CMake
CMake-->IDE{IDE}
IDE-->VisualStudio
IDE-->QtCreator
IDE-->CLion
CMake-->Editor{Text Editor}
Editor-->Vim
Editor-->VSCode[Visual Studio Code]
GTest[Google test & Doxygen]-. integrate .->VisualStudio
GTest[Google test & Doxygen]-. integrate .->VSCode
GTest[Google test & Doxygen]-. integrate .->CLion
GTest[Google test & Doxygen]-. integrate .->CMake
subgraph tooling
Additional[Additional tooling]---ReSharper
Additional---ClangTidy
Additional---ClangFormat
Additional---Incredibuild
end
- The definitive guide of setting up C/C++ development environment on Windows
- Setting up development environment
- Debugging
- Using libraries
- Unit Testing
- Documentation
- Source control
- Setting up a system-wide package manager
- Setting up WSL
- Addtional Tooling
This section describes the steps to
- Download & Install a C++ compiler
- Download & Install CMake
- Download & Install an IDE/text editor
- Create a project in various IDEs/text editors and start writing code
This guide will cover the installation of GCC
, Clang
and MSVC
.
Just launch the installer and keep clicking "Next"
If you also want to install clang
, skip this part and go directly to Install Clang, because GCC is a dependency of Clang (on MSYS2) and will be automatically installed when you install clang.
- Run MSYS2, type the following command:
pacman -Syu
pacman
is the package manager used by MSYS2. -S
means "sync". -y
means "download fresh package databases from the server". -u
means "upgrade installed packages".
This command will update the packages info, so you get the latest packages. It will prompt you like this, and you type y
and hit enter.
- Then it will prompt you
To complete this update all MSYS2 processes including this terminal will be closed. Confirm to proceed [Y/n]
, typey
and hit enter, and it will close the window after the update is done. - Relaunch MSYS2 from your start menu. Type:
pacman -S mingw-w64-x86_64-gcc
like this, type y
and hit enter to install gcc
And then type:
pacman -S mingw-w64-x86_64-make
And type y
to also install make
.
And then type:
pacman -S mingw-w64-x86_64-gdb
And type y
to also install gdb
.
-
Click
Environment Variables
, findPath
inSystem variables
, double click to open the setting. -
Click
OK
to close all windows. Now you finished installing GCC. Open any shell such ascmd
and type ingcc --version
and you shall see the following:
Installing Clang will also automatically install GCC
(on MSYS2).
- Run MSYS2, type the following command:
pacman -Syu
pacman
is the package manager used by MSYS2. -S
means "sync". -y
means "download fresh package databases from the server". -u
means "upgrade installed packages".
This command will update the packages info, so you get the latest packages. It will prompt you like this, and you type y
and hit enter.
-
Then it will prompt you
To complete this update all MSYS2 processes including this terminal will be closed. Confirm to proceed [Y/n]
, typey
and hit enter, and it will close the window after the update is done. -
Relaunch MSYS2 from your start menu. Type:
pacman -S mingw-w64-x86_64-clang mingw-w64-x86_64-clang-tools-extra
like this, type y
and hit enter to install clang
And then type:
pacman -S mingw-w64-x86_64-make
And type y
to also install make
.
And then type:
pacman -S mingw-w64-x86_64-gdb
And type y
to also install gdb
.
-
Click
Environment Variables
, findPath
inSystem variables
, double click to open the setting. -
Click
OK
to close all windows. Now you finished installing clang. Open any shell such ascmd
and type inclang --version
and you shall see the following:
(9. Optional): If you want to get clang's implementation of the C++ standard library (ie. the STL), type this command:
pacman -S mingw-w64-x86_64-libc++
When invoking clang++
, add the flag -stdlib=libc++
.
The header files of libc++
will be stored at C:\msys64\mingw64\include\c++\v1
.
Note: Clang
and GCC
is installed to the same directory, eg. under C:\msys64\mingw64\bin
. Don't be confused by the directory C:\msys64\clang64
. It is an empty folder.
MSYS2 is a collection of tools and libraries providing you with an easy-to-use environment for building, installing and running native Windows software.
But basically, we use its implementation of MingW(Minimalist GNU for Windows), which is a collection of common developing tools seen on GNU/Linux operating systems.
Warning
Please DO NOT use this Mingw-w64 installer, because it uses out-dated GCC toolchain, and is no longer maintained
MSYS2 (in this guide) is actively maintained and provides an up-to-date GCC toolchain as well as many others, is the prefered choice.
Copied from this stackoverflow answer
The GCC compiler in /usr/bin produces executables that use msys-2.0.dll as a runtime dependency. That DLL is basically a fork of Cygwin, and it provides emulation of POSIX commands not normally available on Windows. That environment is mainly for running programs from the Linux world (like bash) which need POSIX commands and cannot be easily ported to a native Windows environment.
The GCC compilers in /mingw32/bin and /mingw64/bin produce native Windows executables targeting the 32-bit or 64-bit versions of Windows respectively. The 32-bit executables can actually run on 32-bit or 64-bit Windows. These executables are easier to distribute; you generally just copy all the DLLs that they depend on from the /mingw*/bin folder to the same directory as your executable, and then you have something that will run successfully on other computers. Since the main purpose of MSYS2 is to help write native Windows software, you'll find a much wider variety of libraries in the MinGW environments than in the msys-2.0.dll environment.
MSVC is Microsoft Visual C++ compiler. And you know what? You do NOT have to install Visual Studio in order to get MSVC. However, if you also want Visual Studio, skip to setting up visual studio directly.
- Download MSVC, select
Build Tools for Visual Studio 2019
- Launch the installer and select these workflows
- You have finished installing MSVC. Click
Launch
and typecl
and you should see this:
Do NOT try to add MSVC
directly to system PATH
because each compiler toolchain for different architecture has its own version.
This command prompt is specific to 64bit Windows architecture and has set some temporary environment variables. You can find it in Start
-> Visual Studio 2019
-> Developer Command Prompt for VS 2019
like this:
After MSVC is installed, cmake can detect it as a compiler.
You can either install CMake by using the official installer or using a package manager like MSYS2
,
which you used to install GCC
and Clang
.
- Using the installer:
-
Download here, choose the
Windows win64-x64 Installer
option -
Launch the insatller, when you see this screen, choose
Add CMake to the system PATH for all users
Now you finished installing cmake.
-
- Using
MSYS2
:- Run
MSYS2
and type this command and typeY
to install
pacman -S mingw-w64-x86_64-cmake
- Run
CMake is a cross-platform build-system generator, which generates build files (some files dictating how your source files should be built) for your platform.
For example, on Windows by default, it generate Visual Studio Solutions
(which is some files dicating how your source files should be built, native to Visual Studio
) if you have Visual Studio installed. On Linux by default, it generates Unix Makefiles
(which is some files dictating how your source files should be built, native to make
).
And because:
It is a bug if your C/C++ project does NOT provide CMake support.
In reality, some of the most loved IDEs/text editors really just provide good CMake support out-of-the-box. So don't argue that Meson is better or some nonsense. To be considered "better", you really have to get good tool chain support.
This guide will cover setting up Visual Studio
, CLion
, QtCreator
, Cevelop (based on Eclipse)
and Eclipse
.
-
Download clion
-
Launch the installer, keep clicking "Next". When you see the following screen, I strongly recommend you to select
Add "Open Folder as Project"
. -
Run clion, set up the appearance as you like, login your account or free trial.
-
After those, it will prompt this window for setting up compilers, it should be all correct and no need to change.
-
Clion will auto generate a "Hello world" project and everything should be working.
- Download QT installer here
- Launch the installer and you will need to either sign in or create a qt account
- When you see this, click
Custom Install
. - When you see this, click
Deselect All
, because we only intend to use it as a standalone IDE, akaQt Creator
. If you want to do Qt development, select the component to your need. - After the installation, run
Qt Creator
->New File or Project
->Non-Qt Project
->Plain C++ Application
(this actually doesn't matter, you can always change to a C application in theCMakeLists.txt
file) -> ChooseCMake
as the build system -> selectall kits
(this will include the different build types in CMake).Qt Creator
should create a simple "Hello world" program for you, like this: - Click the
Run
button, and it should run
Note: If there is error during CMake's configure, go to Tools
-> Options
-> Kits
-> Desktop(default)
, and make sure the C and C++ compiler is in C:\msys64\mingw64\bin
instead of C:\msys64\usr\bin
.
If you installed Clang
you shall see it in the compiler selection menu:
You can install Visual Studio as a standalone IDE or as a whole package including compiler, toolchain and windows sdk.
-
Download Visual studio. Choose the
Community
option. -
After installation, you are prompt to restart your computer. And then you will need to register a Microsoft Account to continue using Visual Studio.
-
Run Visual Studio, select
Create a new project
->Empty Project/Console App
, and selectPlace solution and project in the same directory
. The only difference betweenEmpty Project
andConsole App
is the latter will provide you with a "Hello world" program and that's it! All the default include directories and default linked runtime libraries are the same!
-
If you choose to create
Empty Project
, right click on the<Project Name>
->Add
->New item
->C++ source file
->Add
, like this: Then write a simple "Hello world" program and hitctrl+f5
to compile and run it, and you shall see this: -
If you choose to create
Console App
, you shall see the already created "Hello world". Hitctrl+f5
to compile and run the program and you shall see this:
If you install Visual Studio as a standalone IDE without installing MSVC compiler toolchains, you can use it with CMake. If you have installed MSVC compiler toolchain, you can use it with Visual Studio solution just as it's a full install like above. Here I introduce how to use it with CMake, without MSVC.
-
Download Visual studio. Choose the
Community
option. -
Run the installer, select these workflows and deselect all the optionals on the right, like this
-
After installation, you need to register a Microsoft Account to continue using Visual Studio.
-
Run Visual Studio, select
Create a new project
->CMake Project
-> selectPlace Project under the same directory
->Create
, like this: -
Visual Studio will auto generate a "Hello world" project for you, and it can successfully configure the project and compile because CMake can detect the installed
GCC
. However, it will have incorrect include errors. -
To solve this error, click on the configuration menu ->
Manage Configurations
-> click the add button -> selectMingw64-Debug
-> click on the previous old configuration and click delete button -
Hit
ctrl+s
to save this configuration, then the include error should go away.
Note: If for some reason, Visual Studio doesn't detect the right MingW version, you will still get include errors. You need to edit the CMakeSettings.json
and correct the MingW version, like this:
-
Download vscode
-
Launch the installer, when you see this screen, I strongly recommend you follow this setting
-
Run vscode, in the
extension
tab, search and install the following extensions
- Install
Microsoft C/C++
extension, It is a Language Server by Microsoft. You can also install llvm's LSPclangd
.clangd
will have limitions when using with Visual Studio as CMake's generator. See the discussion here. - And 2 extensions for cmake. The first one in the list is for syntax highlighting when writing cmake scirpts.
- The second one in the list is for actually running Cmake.
-
Go to settings, search
generator
. And setCmake:Generator
toMinGW Makefiles
, like this: -
Create a folder, open it in vscode. Use
ctrl + shift + p
to open the command menu, typecmake
and chooseCMake: Quick Start
, like this: -
The cmake tool will scan the kits and there will be 2 kits. Select the first one.
-
Type a name for your project, select
Executable
, CMake tool will automatically generate a helloworld project for you. And you probably don't want to enable ctest for now, so delete everything excpet the following 3 lines:
cmake_minimum_required(VERSION 3.5.0)
project(helloworld VERSION 0.1.0)
add_executable(helloworld main.cpp)
Important
Rememeber to click Allow
when cmake want to configure the intellisense.
- And now you can run it and debug it, and have everything working (syntax highlighting, auto complete, header files...).
- If you install
vim
inMSYS2
, your.vimrc
file should be placed inC:\msys64\home\<UserName>\.vimrc
- Create new folders along this path
and then open
C:\msys64\home\<UserName>\.vim\autoload
powershell
here. - Type the following command to install
vim-plug
, a simple vim plugin manageriwr -useb https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim |` ni plug.vim -Force
Download and install Vim here. It can be installed by keep clicking "Next" in the installer. (Note: Recommended method to install vim is through a package manager, see here)
- Install
vim-plug
iwr -useb https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim |`
ni $HOME/vimfiles/autoload/plug.vim -Force
Open .vimrc
, add these following lines:
filetype plugin indent on
set tabstop=4 " show existing tab with 4 spaces width
set shiftwidth=4 " when indenting with '>', use 4 spaces width
set expandtab " On pressing tab, insert 4 spaces
syntax on
set nu " Enable line numbers
set smartindent
call plug#begin('~/.vim/plugged') " List all the plugins to be installed
Plug 'neoclide/coc.nvim'
call plug#end() " End of plugins
-
Install
coc-clangd
, the C++ language client extensionOpen whatever C++ file, and type command
:CocCommand clangd.install
-
Install
clangd
, the actual C++ language server.Type command
:CocCommand clangd.install
.
All the plugins listed below can be installed by adding Plug '<plugin-repo>'
betweeen the call plug
and call plug
lines in the .vimrc
file.
- vim-airline An enhanced status line.
Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes'
Plug 'preservim/nerdtree'
Plug 'Xuyuanp/nerdtree-git-plugin'
Plug 'tiagofumo/vim-nerdtree-syntax-highlight'
Plug 'ryanoasis/vim-devicons'
This section describes how to debug in various IDEs/text editors.
To launch the debugger in VSCode, click the cmake project menu -> right click on the <target name>
-> Debug
like this:
See more documentation for VSCode's debugging UI here, except for the part that sets launch.json
because the CMake tools already handles everything :)
For more, see documentation here
For more, see documentation here
vcpkg
is a C/C++ package manager, which makes using libraries much easier (almost as easy as using pip
in python).
You HAVE TO install MSVC or Visual Studio on Windows to use vcpkg
.
(Mingw GCC CAN NOT be used to build vcpkg
on Windows at the time being).
After MSVC is installed, you can follow the guide here
to set it up.
Starting from this commit, vcpkg
binary can be
directly downloaded by running bootstrap-vcpkg.bat
, you no longer need to install MSVC to build it!
- Open a shell(
cmd
) and go to the directory where you wantvcpkg
to be installed. (Something likeC:\
orC:\dev
) - Type this command:
git clone https://github.com/microsoft/vcpkg
- Type this command:
.\vcpkg\bootstrap-vcpkg.bat
- Type this command:
.\vcpkg\vcpkg.exe integrate install
- To find a library, use
vcpkg search <library>
- To install a library, use
vcpkg install <library>:x64-windows
orvcpkg install <library>:x86-windows
Note
vcpkg
will build 32 bit libraries by default on Windows (although it's 64 bit on Linux by default,Microsoft fix it please), which is NOT probably what you want, so you want to speficy the architecture by adding :x64-windows
.
After you install the library in vcpkg
, you either:
- Use
Visual Studio
without ANY ADDITIONAL CONFIGURATION - Use
cmake
with the instruction provided byvcpkg
when you install the library.
Below is a complete example of using vcpkg
to install and use the boost library.
-
Install the library in
vcpkg
withvcpkg install <Library Name>
, like this:vcpkg install boost:x64-windows
-
Note that on Windows,
vcpkg
builds libraries usingMSVC
, so you should also useMSVC
in order to link sucessfully. Header-only libraries likeboost
may be used with other compilers likeGCC
.
Afrer the library finishes installing, you can either:
-
Use it in Visual Studio without doing any additional configuration Note: Configure the solution achitectural target correctly according to your library. Visual Studio empty project defaults to
x86
but you may installedx64
library. -
Or use it in VSCode/CLion with cmake and cmake tool chain file. See the docs here
google test is a famous and widely supported by IDEs/text editors unit testing framework for C++.
You can get google test by these ways
-
Using
vcpkg
: Following setting upvcpkg
, we can easily install the library byvcpkg install gtest:x64-windows
Note that if your application is targeted to 32 bit, use this command instead
vcpkg install gtest
-
Using
MSYS2
: Note that this can only be used withGCC & Clang
compiler fromMSYS2
.pacman -S pacman -S mingw-w64-x86_64-gtest
After installing the library,
-
If you use Visual Studio (MSBuild Project), you just need to
#include <gtest/gtest.h>
like a normal C++ source file and either:-
Provide a
main
function at the bottom of your source fileint main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }
-
Don't provide a
main
function, then you need to link additional libraries in the linker settings.- Right click on your project ->
Properties
->Linker
->AdditionalDependencies
, Make sure this configuration isDebug
andx64
(or x86 depend on the architect or your installed Gtest library) and add these 2 lines
gtestd.lib $(VcpkgRoot)installed\$(VcpkgTriplet)\debug\lib\manual-link\gtest_maind.lib
- Click the configuration menu to
Release
and also add these 2 lines, like this
gtest.lib $(VcpkgRoot)installed\$(VcpkgTriplet)\lib\manual-link\gtest_main.lib
Then you should be able to write the test source file without the
main
function, and build in both configurations like this - Right click on your project ->
-
-
If you use CMake, regardless of whether you installed
Google Test
library fromvcpkg
orMSYS2
, you can make use ofCTest
built-in to Cmake as a test runner to run your google test, which is supported by most IDE/editors you will see below. A minimumCMakeLists.txt
is like:
cmake_minimum_required(VERSION 3.10.0)
project(<project name> VERSION 0.1.0)
find_package(GTest CONFIG REQUIRED)
enable_testing()
include(GoogleTest) #for gtest_discover_tests() function
add_executable(<test target name> test.cpp) #This is the testing executable
target_link_libraries(<test target name> PRIVATE GTest::gtest GTest::gtest_main) #Link it to the google test library
gtest_discover_tests(<test target name>) #integrate google test with ctest to this testing executable
- Or you simply want a testing executable, so you don't bother with
CTest
.
cmake_minimum_required(VERSION 3.10.0)
project(<project name> VERSION 0.1.0)
find_package(GTest CONFIG REQUIRED)
add_executable(<test target name> test.cpp) #This is the testing executable
target_link_libraries(<test target name> PRIVATE GTest::gtest GTest::gtest_main) #Link it to the google test library
CLion has test adaptors built-in so it should automatically detect the test whether you are using CTest
as runner or just simply compiling a testing executable.
You can click the run
button on the left of each TEST()
macro to run individual test, or click ctrl+shift+f10
to run all test.
See here for more documentation.
You need to use CTest
(the first version of the minimum CMakeLists.txt
) as your test runner to get the integration working.
- Install the CMake Test Explorer extension (proud contributor)
- Open VSCode settings, go to
Extension
->CMake Test Explorer
section, and change these following settings: - After that, build your project once and then click the
refresh test
button, this plugin should find all the testing suites and test cases in your test files. - Then you can easily manage or debug all your test cases or each individual test in this panel.
Writing good documentation is also an essential part of development. The most commonly used documentation generator is doxygen. Download the binary distribution for Windows
and then install it. After it is installed, there will be a GUI frontend called doxywizard
, which looks like this:
To write good documentation, install these plugins:
- For VSCode (proud contributor)
- For Visual Studio
Learn the syntax for documentation here
After you documment your code, any decent IDEs/text editors should be able to show the documentation, helping you better understand your own code as well as others.
Using doxygen is straight-forward using the GUI, just specify the root directory of your project, configure some settings to your liking, then run it.
Doxygen generated documentation too ugly? Follow the guide here to use doxygen with sphinx for a more beautiful documentation.
You can set up an automatic documentation generation step within CMake, so that each time you build your cmake project,
the docs would be generated or updated.
To do that, assuming you have a doc
directory in your project, something like this
MyProject
|--doc
|--README.md
|--CMakeLists.txt
|...
add the following snippet to your root CMakeLists.txt
find_package(Doxygen)
if(DOXYGEN_FOUND)
set(DOXYGEN_USE_MDFILE_AS_MAINPAGE "${CMAKE_SOURCE_DIR}/README.md")
set(DOXYGEN_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/doc")
set(DOXYGEN_EXCLUDE_PATTERNS "<your binaries or other files generated by your IDE>")
doxygen_add_docs(doc ${CMAKE_SOURCE_DIR} ALL) #this will create a target called "doc"
#You can either manually run the target or it will automatically run when "cmake --build ." is called
endif()
Most if not all of the development workflow involves using Git
.
Also, some of CMake's functionalities requires Git to be installed.
And you also need Git to install vcpkg
.
You can install Git
either by using the installer or using a package manager,
like MSYS2
which we just used above to install GCC
and Clang
.
- Install by using the installer
- Download the installer here and then it can be installed by keep clicking
Next
.
- Download the installer here and then it can be installed by keep clicking
- Install by using a package manager
- chocolatey:
choco install git
- scoop:
scoop install git
- winget:
winget install git
- chocolatey:
Package manager makes it easier to install and update softwares, allowing you to use one single command to update all installed softwares.
On Windows, there is built-in winget
on a reasonably new build of Windows 10.
I recommend installing those frequently updated software that doesn't have a built-in updater
(like cmake
, vim
...)
using a package manager.
Install and docs here
Install and docs here
Install and docs here
Setting up WSL is the same as setting up a pure linux environment, therefore it is not discussed here.
is a non-free extension for Visual Studio that can greatly benefit your productivity. Download here.
Clang-tidy is a C++ "linter" that provides extra warnings and style checks to your C++ compiler.
Clang-Tidy support is available starting in Visual Studio 2019 version 16.4. It's included automatically when you choose a C++ workload in the Visual Studio Installer. More info.
- Right click on project ->
Properties
->Code Analysis
->Clang-tidy
- In
Checks to Enable or Disable
, you can configure checks to be enabled or disabled using supported flags. To enable a check, add the flag name directly. To disable a check, prefix with a-
. Flags are separated by comma. For example:*
enables all checks.-clang-analyzer-*
disables all checks namedclang-analyzer...
. - Build your project, and you should see warnings provided by clang-tidy.
- Click on build configuration menu ->
Manage Configuration
->Edit JSON
- Add a key named
enableClangTidyCodeAnalysis
and set value totrue
. Optionally control which checks to be enabled or disabled by adding a key namedclangTidyChecks
- Save and exit. Now you should see warnings provided by clang-tidy.
Install this plugin.
ClangFormat is a code formatting tool to help your code follow some pre-defined formatting rules.
In all IDE/editors, the actual ClangFormat executable needs to be installed first.
- Install this plugin at the bottom "Visual Studio plugin installer"
- Then you can find settings in
Tools
->Options
->LLVM/Clang
->ClangFormat
ClangFormat is supported by VSCode C++ extension out-of-the-box. ClangFormat settings can be found in C++ extension settings.
is a free-for-personal-use build tool that accelerate visual studio's project building. It also provides a nice graph to visualize the building process and time consumption of individual files.
It can be installed when installing visual studio. Download the license here.
is a VSCode extension that automatically add include guard for you so that you no longer need to remember it. Download here.
is a VSCode extension that shows the included header file size and provide a fast way to jump to those included files. Download here
is a VSCode extension that provide a fast way to switch between different fonts. Download here.