How to Set Up Visual Studio Code for C & C++ Programming (Step-by-Step Guide)

preview_player
Показать описание
In this video, I walk you through the process of setting up Visual Studio Code (VS Code) to write, compile, and run C and C++ code on Windows. I'll cover everything from installing the Compiler debugger to configuring VS Code, so you can start coding right away.

Timestamps for video topics
00:28 Installing gcc, g++ Compiler and gdb debugger
07:16 Check for proper installation of gcc g++ and gdb
08:28 installing vs code
09:53 setting vs code for C C++ Programming

Commands used
for installing Compiler : pacman -S mingw-w64-ucrt-x86_64-gcc
For Installing Debugger : pacman -S mingw-w64-ucrt-x86_64-gdb

Once MSYS2 is installed, open the MSYS2 terminal. To update the installed packages, run this command:
pacman -Syu

If there are any updates available, MSYS2 will download and install them. After that, close and reopen the terminal to ensure everything is up to date.

After that install the compiler and debugger by using the command provided above.

Confirm the installation by typing "Y" when prompted. If any errors occur, try running the commands again. Once the compiler / debugger is installed, we need to update the PATH environment variable so you can use the compilers from any folder on your system. Go to `C:\msys64\usr\bin`, copy the path, and add it to the system’s PATH variable through the “Edit Environment Variables” window in Windows.

To confirm the installation, open a new terminal window and type:
gcc --version
g++ --version
gdb --version

If the installation was successful, you'll see the version numbers of GCC, G++, and GDB. If you see an error like "command not found," the installation might not have worked properly.

After installing, launch VS Code. Create a workspace and add a folder to the workspace.

create a simple C++ Program.

To compile and run this code, you need to install the C/C++ extension for VS Code. Go to the Extensions tab, search for "C++," and install the "C/C++ Extension Pack" by Microsoft. This will allow VS Code to work with the compilers and debugger.

Once the extension is installed, go back to your code file. Then, to run the code, click on the play button with the down arrow at the top-right of the screen. Choose the "Run C++ File" option, and make sure to select the `g++` compiler. The code will be compiled, and the output will appear in the terminal at the bottom of the VS Code window.

And that’s it! You’ve now set up your system to write, compile, and run C and C++ code in Visual Studio Code. If you found this video helpful, give it a thumbs up. If you have any questions, drop them in the comments below. Don’t forget to subscribe for more tutorials!

Our Website

Social Media
Рекомендации по теме
Комментарии
Автор

Guys
Seriously this worked for me
After trying many videos finally this helped
In other videos they didnt say to install debugger

imshaikrehan
Автор

Thanks mate. With your help finally I could run for the first time a C++ program in VS Code.^^
If you would make a tutorial for activating OMP and MPI in VS Code also, you would be my VS Code hero.^^ 😉

ParalyticAngel
Автор

I am getting a path error when I try to run the code.

Executing task: C/C++: gcc.exe build active file

Starting build...
cmd /c chcp 65001>nul && C:\MinGW\bin\gcc.exe -fdiagnostics-color=always -g -o
The system cannot find the path specified.

I don't have MinGW on my C drive, are we supposed to?

crneymar