C/C++ Installation
I. What do we need?
1/ Editor vs Compiler
- VS Code is just the editor (where u write code). it can't turn
.c/.cppfiles into programs by itself - the compiler does that part. on windows we use gcc / g++ from MinGW-w64, installed through MSYS2
- so: VS Code to write, g++ to compile, the C/C++ extension to connect them
2/ Before u start
- around 1-2 GB free on the C disk
- stable internet (the compiler download is a few hundred MB)
- admin rights on ur computer (needed for the environment variables step)
II. Installing
1/ Visual Studio Code
- go to: https://code.visualstudio.com/
- click on Download for Windows and run the installer
- on the Select Additional Tasks screen, tick Add to PATH and the 2 Add "Open with Code" action boxes (super handy later: right click a folder -> open it straight in VS Code)
2/ MSYS2 (MinGW-w64 compiler)
- go to: https://www.msys2.org/ and download the installer (
msys2-x86_64-*.exe) - run it, click Next and keep the default folder
C:\msys64(don't change it, the PATH step below depends on it) - after installing, tick Run MSYS2 now and click Finish. a black window will pop up
- copy the command below, paste it in with Shift + Insert (
Ctrl + Vdoesn't work in this window) and press Enter:
pacman -S --needed base-devel mingw-w64-ucrt-x86_64-toolchain
- it will ask which packages to install, press Enter to take everything, then type
Yand Enter again to start the download. then wait
3/ Add the compiler to PATH
- caution: this step is a MUST to run C/C++, without it windows has no idea where g++ is
- click on the Windows icon, search for
Environment Variablesand choose Edit the system environment variables - click on Environment Variables...
- -> System variables -> find Path -> choose it and click on Edit...
- click on New and paste this exact thing:
C:\msys64\ucrt64\bin - click on OK again and again to close everything
- close and reopen every terminal / VS Code window, they only read PATH when they start
4/ Check g++
- open Command Prompt (Windows ->
cmd-> Enter) - paste these in and press Enter:
gcc --version
g++ --version
gdb --version
- if the output is, for example,
g++.exe (Rev1, Built by MSYS2 project) ...then ure done gdbis the debugger, VS Code needs it when u debug
5/ C/C++ extension for VS Code
- Visual Studio Code -> Ctrl + Shift + X (Extensions)
- search for
C/C++ - select and install C/C++ (Microsoft)
- then u might want to restart your whole computer to make sure everything gonna be okay (pray)
III. First program
1/ Create a folder
- make a folder for ur code, e.g.
D:\code\cpp - avoid spaces and Vietnamese characters in the path (e.g.
D:\Tài liệu\code c++), the compiler and the debugger can act weird with them - open it in VS Code: File > Open Folder...
2/ hello.cpp
- create a file
hello.cpp:
hello.cpp
#include <iostream>
using namespace std;
int main() {
cout << "hello from c++" << endl;
return 0;
}
3/ Compile and run
- open the terminal inside VS Code: Ctrl + `
- compile:
g++ hello.cpp -o hello
- run:
.\hello
- for C it's the same idea, just use
gcc:gcc hello.c -o hello - useful flags:
-Wall(show warnings),-std=c++17(pick the c++ standard),-O2(optimize), e.g.g++ -std=c++17 -Wall hello.cpp -o hello
4/ Run button (optional)
- open
hello.cpp-> click the ▷ on the top right -> Run C/C++ File -> choose g++.exe - the first time, VS Code creates a
.vscode/tasks.jsonin ur folder, that's normal
IV. Common errors
'g++' is not recognized as an internal or external command: PATH is wrong or u didn't reopen the terminal. re-check step II.3, the path must be exactlyC:\msys64\ucrt64\bin- the .exe window opens and closes right away: u double-clicked it. run it from the terminal instead
cannot open output file hello.exe: Permission denied: the oldhello.exeis still running. close it (or Ctrl + C in the terminal) and compile again- antivirus blocks / deletes ur .exe: happens sometimes with fresh exe files, add ur code folder to the exceptions
#include <bits/stdc++.h>: works here (it's a g++ thing), a lot of competitive programmers use it
So overall: VS Code to write, g++ to compile, the terminal to run. once g++ --version works, ur good to go
V. Reference Sources
- Visual Studio Code Docs. Using GCC with MinGW. code.visualstudio.com
- MSYS2. Getting Started. msys2.org