Caution

A friendly reminder: The following tutorial is for Linux user instead of Windows

Tutorial

If you want to debug your c++ code with GDB

You can read this ( Debugging your code ) article from Visual Studio Code official website.

Step 1: Compilation

You need to set up task.json for compilation of your cpp file

or simply type in the following command in the command window

g++ -g file.cpp -o file.exe

to generate a debuggable .exe file

Step 2: Set up the launch.json file

To enable debugging, you will need to generate a launch.json file

follow the launch.json example or google others

Step 3: Press (Ctrl+F5) to start compiling

this launch.json file will launch the configuration when you press the shortcut (Ctrl+F5)

Enjoy it!

ps. For those who want to set up tasks.json, you can read this from vscode official (-> TypeScript Hello World)

Answer from WY Hsu on Stack Overflow
🌐
Visual Studio Code
code.visualstudio.com › docs › cpp › config-mingw
Using GCC with MinGW
November 3, 2021 - You can view the C/C++ configuration UI by running the command C/C++: Edit Configurations (UI) from the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)). This opens the C/C++ Configurations page. When you make changes here, VS Code writes them to a file called c_cpp_properties.json in the .vscode folder. Here, we've changed the Configuration name to GCC, set the Compiler path dropdown to the g++ compiler, and the IntelliSense mode to match the compiler (gcc-x64).
🌐
YouTube
youtube.com › watch
How to Set up VS Code for C and C++ Programming & install GCC on Windows. - YouTube
How to Set up Visual Studio Code for C and C++ Programming & Install Gcc on windows.In this video, I have shown how you can install gcc and g++ compilers to ...
Published   September 13, 2021
🌐
YouTube
youtube.com › techie programmer
Installing GCC on Windows 10 with VS Code, (2021) Made Easy | Techie Programmer - YouTube
MingGW installation on Windows 10. Setting up your Windows environment to run C C++ programs with VS Code Editor In this video I show you how to correctly in...
Published   April 14, 2021
Views   6K
🌐
Reddit
reddit.com › r/learnprogramming › compiler for c/c++ with vscode
r/learnprogramming on Reddit: Compiler for C/C++ with VSCode
September 18, 2023 -

I already have CodeBlocks (an open source IDE) installed on windows 10. I am practicing basic C programs on it.

Now, I wanted to install VSCode for python, etc. So I installed it and the python extension alongside.

Then I see that it also has support for C/C++ with another extension. It would be nice to use only VSCode for both python and C/C++ so I installed the extension. But it also needs a compiler for C/C++.

I have found some instructions on installing the C/C++ compiler/s here. The process is more complicated than I expected.

  1. Can I somehow use the MinGW compiler that CodeBlocks already has to compile in VSCode?

  2. Should I just stick to non- C/C++ programming with VSCode and keep using CodeBlocks for when I am doing C?

Appreciate any advice.

Edit: SOLVED imgur link

Top answer
1 of 4
2
Can I somehow use the MinGW compiler that CodeBlocks already has to compile in VSCode? Yes. The easiest would be to add MinGW from CodeBlocks into your PATH, so VSCode can automatically detect and use it if C++ extension is available. In CodeBlocks go to Settings -> Compiler settings -> Toolchain Executables tab, and there will be a "Compiler's installation directory" with a path of a folder in which MinGW is installed by CodeBlocks installer. Then, you can add this folder to your PATH, see for example this for how to do it: https://docs.oracle.com/en/database/oracle/machine-learning/oml4r/1.5.1/oread/creating-and-modifying-environment-variables-on-windows.html#GUID-DD6F9982-60D5-48F6-8270-A27EC53807D0 . Please ADD things to your PATH, DO NOT REPLACE ANYTHING OR THINGS WILL BREAK. Then, close all command prompt (or PowerShell or Terminal) windows and open a new one and run the gcc command -- if it shows "not recognized as internal or external command", something is wrong, in other case gcc will run and tell you you forgot some parameters. And now it should also work in VSCode and can be automatically recognized.
2 of 4
1
On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge. If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options: Limiting your involvement with Reddit, or Temporarily refraining from using Reddit Cancelling your subscription of Reddit Premium as a way to voice your protest. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
🌐
Luis Llamas
luisllamas.es › inicio › cursos › curso c++
How to use Visual Studio Code and GCC with C++
December 7, 2024 - Additionally, we will need to install the VSCode extension for C++. Visual Studio Code does not include a compiler, so we need to install one. On Windows systems, the most common option is MinGW, while on macOS and Linux, the GCC compiler can be used.
🌐
Studocu
studocu.com › delhi technological university › programming fundamentals › vscode setup guide for gcc on windows
VSCode Windows - Install GCC Compiler: Go to ...
November 4, 2022 - How to install VS Code in Windows Environment. Fully explained with steps and setting up environment variables etc, with screenshots. install gcc compiler: go
Find elsewhere
🌐
DEV Community
dev.to › bilalmohib › configuring-cc-on-windows-for-vs-code-2foe
Configuring c/c++ on windows for VS Code. - DEV Community
December 6, 2021 - Follow these steps to configure VS code for c/c++ on windows. To install gcc compiler for windows you can use MSYS2.It consists of bundle of tools and will install gcc also. You can download this from the link below.
🌐
Reddit
reddit.com › r/programminghelp › how to run gcc on windows 11? i installed it (correctly) based on the instructions on msys2 and visual studio's websites, but it doesn't execute code
r/programminghelp on Reddit: How to run GCC on Windows 11? I installed it (correctly) based on the instructions on MSYS2 and Visual Studio's websites, but it doesn't execute code
February 17, 2022 -

Not sure if this counts, since this is more about running code than writing it.

I have followed the instructions on these sites. I have also made sure to change "Path" in the environmental variables. https://code.visualstudio.com/docs/cpp/config-mingw https://www.mingw-w64.org/

The installation worked, but no matter what I try (running in command prompt, running in Visual Studio) I get something like: "'g++' is not recognized as an internal or external command, operable program or batch file." when I try to execute some code. Why is this?

EDIT 1: Visual Studio Code*, not Visual Studio.

EDIT 2: Seems to be working now! I'm the only user on my computer, but I had to change my system variables - not just the user variables - for it to work.

🌐
Visual Studio Code
code.visualstudio.com › docs › languages › cpp
C/C++ for Visual Studio Code
November 3, 2021 - Select the Extensions view icon on the Activity Bar or use the keyboard shortcut (⇧⌘X (Windows, Linux Ctrl+Shift+X)). Search for 'C++'. Select Install.
🌐
Medium
techifyshreyansh2105.medium.com › installation-of-vs-code-g-compiler-and-setup-environmental-variables-975894adb30f
INSTALLATION OF VS CODE, g++ COMPILER AND SETUP ENVIRONMENTAL VARIABLES | by TECHIFY SHREYANSH | Medium
May 8, 2021 - Visual Studio Code places these settings in .vscode\c_cpp_properties.json. If you open that file directly, it should look something like this: { "configurations": [ { "name": "GCC", "includePath": ["${workspaceFolder}/**"], "defines": ["_DEBUG", "UNICODE", "_UNICODE"], "windowsSdkVersion": "10.0.18362.0", "compilerPath": "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/g++.exe", "cStandard": "c11", "intelliSenseMode": "gcc-x64" } ], "version": 4 }
🌐
Medium
medium.com › @burakeryavuz › how-to-write-c-code-on-visual-studio-code-using-gcc-compiler-mac-m1-m2-775de8803b0f
How to write C code on Visual Studio Code using GCC Compiler (MAC M1/M2) | by Burak Eryavuz | Medium
September 28, 2023 - Find the C/C++ plugin and install it by clicking the “Install” button. Open a C/C++ project in VS Code or create a new one. Create a folder named .vscode in your project folder (if you don’t already have one).
Top answer
1 of 1
4

Visual Studio Code is a cross-platform IDE that uses a tasks.json file to describe how to compile (and perform other tasks) your project. You can run the VSCode in the Windows or in the WSL Ubuntu subsystem. If you wanna run it into the WSL, you must use the Ubuntu/Linux binaries.


Notes on WSL

In the Windows command line, you can run linux commands using bash

C:\> bash.exe -c <linux command>

For instance, you can run the Linux gcc using

C:\> bash -c "gcc -v"

If you have installed multiple linux systems on WSL, e.g. an opensuse and a ubuntu, you must use opensuse-42 run or ubuntu run instead of bash to determine which linux subsystem use at the windows command line.

C:\> ipconfig | opensuse-42 run grep IP | ubuntu run lolcat

In addition, note that the Windows file system can be accessed using /mnt/<drive letter>/. For instance, if you have a C:\Projects folder, you can access it from Linux at /mnt/C/Projects


Configuring a VSCode in Windows to use the GCC in the WSL Linux

Check the instructions in the website. To use GCC or CLang compilers in Mac or Linux, you may use the bash with different arguments depending on the task.

You can configure (or create) your own task.json. You must define the bash as the command to use. I think almost the same configuration you use for Mac/Linux must work. I changed the "cwd" option.

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "0.1.0",
    "wsl": {
        "command": "bash",
        "args": ["-c"],
        "isShellCommand": true,
        "showOutput": "always",
        "suppressTaskName": true,
        "options": {
            "cwd": "/mnt/C/${workspaceRoot}"
         },
        "tasks": [
             {
                "taskName": "hello",
                "args": [
                    "make hello"
                ],
                "isBuildCommand": true
             },
             {
                "taskName": "clean",
                "args": [
                    "make clean"
                ]
             },
             {
                "taskName": "compile w/o makefile",
                "args": [
                    "g++ helloworld.C -o hello"
                ],
                "echoCommand": true
            }
        ]
    }
}

There are some other Gists you can use as other examples.

NOTE: I have found some MS tutorials on compiling and debugging Linux GCC programs using WSL and Visual Studio but not Visual Studio Code.

🌐
Studocu
studocu.vn › trường đại học bách khoa hà nội › data structures and algorithms › c/c++ development setup: vs code & gcc installation guide
C/C++ Development Setup: VS Code & GCC Installation Guide - Studocu
June 30, 2023 - e. Click on Teminal > New Terminal to open Terminal. f. In the terminal window, type “gcc hello” to compile the hello file. By default it will generate a file as a result (can type “ls” to see all files).
🌐
Aleksandar Haber
aleksandarhaber.com › how-to-install-c-compiler-and-use-it-in-vs-code-in-windows
How to Install C++ Compiler and Use it in VS Code in Windows – Fusion of Engineering, Control, Coding, Machine Learning, and Science
To do that in Windows, click Start->”Edit the System Environment Variables”. Then, click on “Advanced”, then click on “Environment Variables”. Then click on “Path”, then “edit”, then “new”, and insert the path to the folder. That is, you should insert the complete path ...
🌐
Reddit
reddit.com › r/vscode › vscode can't find gcc
r/vscode on Reddit: VSCode can't find gcc
March 12, 2022 -

Hey all,

I'm relatively new to C++ and have installed the basic extensions for it (see the list in the packages sidebar). At first everything worked, but then I had to turn of my computer for a week as I was moving and, when I turned it back on, it could no longer find gcc.

For clarity this is a custom built PC running windows 10 Pro and the newest version of unbutu via WSL.

This bug is preventing me from including anything in the standard namespace, so it's pretty severe. Oddly enough though, gcc does indeed exist at this location and I can use it to compile.

Any suggestions on how to get VSCode to see it again?

VSCode c_cpp_properties.json File exists according to Cygwin

WSL recognizes g++ (tested gcc as well; it works too)
Top answer
1 of 4
5

Hey,,, It will work for you I believe

Blockquote

  1. First: open the link: https://www.msys2.org/
  2. Go to the installation and download the installer .. install the exe file.
  3. search mingw in windows search(windows start)
  4. there is an app named--> MSYS2 MinGW {64/32}-bit
  5. run it as administrator
  6. Found command shell ---> type: pacman -Syu
  7. Will ask you for some installation click on Y
  8. Then again go to the same app MSYS2 MinGW {64/32}-bit and run as admin
  9. and write--> pacman -Ss gcc (in shell that opend after click)
  10. after clicking there will be bunch of things and now you have to care about your windows arch.. if it is 64 bit then write---> pacman -S mingw-w64-x86_64-gcc (and if its 32 you will find something like that where 64 will replaced by 32 in bunch of commands that are showing in your shell)
  11. after executing this command gcc will be installed in your system to check write: gcc --version || g++ --version
  12. After that to install the debugger write pacman -S mingw-w64-x86_64-gdb
  13. to check write: gdb --version

**Every thing got installed in your system now find the mingW file or mysys2 file on C drive(whereever you r mingW file got saved) go the the minGW 64 || 32 accroding to your operating system there is a bin folder click on that and copy the path inside the bin folder and save it in environment variables path **

Blockquote

Enjoy vs code

2 of 4
4

Works perfect in Visual Code. You need the following:

  • C/C++ extension in Visual Code.
  • msys64 installed in C:\
  • Add path to msys64 bin folder in environment variable

Instructions:

https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools

Important the msys64 path doesn't contain spaces so don't install it in Program Files.

You can search "Edit system environment variables" from the windows button, press button Environment Variables, and then add it as a System variable "Path". For me its located in

C:\msys64\mingw64\bin
🌐
Visual Studio Code
code.visualstudio.com › docs › cpp › config-wsl
Using C++ and WSL in VS Code
November 3, 2021 - As you go through the tutorial, you will see three files created in a .vscode folder in the workspace: c_cpp_properties.json (compiler path and IntelliSense settings) ... In the File Explorer title bar, select the New File button and name the file helloworld.cpp. Once you create the file and VS Code detects it is a C++ language file, you may be prompted to install the Microsoft C/C++ extension if you don't already have it installed.