C++ with VS Code
C/C++ for Visual Studio Code
https://code.visualstudio.com/docs/languages/cpp
VS Code is an editor, NOT IDE. It does not have C++ compiler or debugger. You will need to install these tools or use those already installed on your computer.
For Window:
GCC, MSVC
MSVC is installed when Visual Studio Community is installed.
MacOS:
GCC, Clang
Steps
Install C++ Extension in VS Code
Install Compiler (GCC)
Step 1: Install C++ Extension in VS Code
Open VS Code.
Select the Extensions view icon on the Activity bar or use the keyboard shortcut (Ctrl+Shift+X).
Search for
'C++'
.Select Install.
After you install the extension, when you open or create a *.cpp
file, you will have syntax highlighting (colorization), smart completions and hovers (IntelliSense), and error checking.
Step 2: Install Compiler
Lets use GCC for this tutorial.
Check if you have a compiler installed
Make sure your compiler executable is in your platform path (%PATH
on Windows, $PATH
on Linux and macOS) so that the C/C++ extension can find it. You can check availability of your C++ tools by opening the Integrated Terminal (Ctrl+`) in VS Code and trying to directly run the compiler.
Checking for the GCC compiler g++
:
Checking for the Clang compiler clang
:
Install MinGW-x64 for GCC
Follow the Installation instructions on the MSYS2 website to install Mingw-w64.
Take care to run each required Start menu and
pacman
command.
Open MSYS2 with terminal for the UCRT64 environment
Install mingw-w64 GCC by
pacman -S mingw-w64-ucrt-x86_64-gcc
You will need to install the full Mingw-w64 toolchain to get the gdb
debugger.
pacman -S --needed base-devel mingw-w64-x86_64-toolchain
Then, select mingw-w64-x86_64-gbd
Add the MinGW compiler to your path
Add the path to your Mingw-w64 bin
folder to the Windows PATH
environment variable by using the following steps:
In the Windows search bar, type 'settings (설정)' to open your Windows Settings.
Search for Edit environment variables for your account. (계정의 환경 변수 편집)
Choose the
Path
variable in your User variables and then select Edit.Select New and add the Mingw-w64 destination folder path:
C:\msys64\mingw64\bin
.Select OK to save the updated PATH. You will need to reopen any console windows for the new PATH location to be available.
To check your MinGW installation, open a new Command Prompt and type:
Test Code
We'll create the simplest Hello World C++ program.
Create a folder called "HelloWorld" and open VS Code in that folder (code .
opens VS Code in the current folder):
Accept the Workspace Trust dialog by selecting Yes, I trust the authors
You can also open VS Code directly.
Now create a new file called helloworld.cpp
with the New File button in the File Explorer or File > New File command.
Now paste in this source code:
Now press Ctrl+S to save the file. You can also enable Auto Save to automatically save your file changes, by checking Auto Save in the main File menu.
Build Hello World
Now that we have a simple C++ program, let's build it. Select the Terminal > Run Build Task command (Ctrl+Shift+B) from the main menu.
Choose GCC toolset MinGW: C/C++: g++.exe build active file.
This will compile helloworld.cpp
and create an executable file called helloworld.exe
, which will appear in the File Explorer.
Run Hello World
VS Code Integrated Terminal, run your program by typing ".\helloworld".
You can also run by Ctrl+F5
If everything is set up correctly, you should see the output "Hello World".
Last updated