Using GCC with MinGW
AI agents: See llms.txt for documentation discovery and navigation. Documentation articles are also available as Markdown by appending .md to the article URL. Join VS Code at Microsoft Ignite or catch the sessions online. A year of VS Code, wrapped. Explore the highlights. Using GCC with MinGW View as Markdown Ask in VS Code Ask in VS Code Insiders In this tutorial, you configure Visual Studio Code to use the GCC C++ compiler (g++) and GDB debugger from mingw-w64 to create programs that run on Windows.
After configuring VS Code, you will compile, run, and debug a Hello World program. This tutorial does not teach you about GCC, GDB, minGW-w64, or the C++ language. For those subjects, there are many good resources available on the Web. If you have any problems, feel free to file an issue for this tutorial in the VS Code documentation repository . Prerequisites To successfully complete this tutorial, you must do the following steps: Install Visual Studio Code .
Install the C/C++ extension for VS Code . You can install the C/C++ extension by searching for 'C++' in the Extensions view ( ⇧⌘X (Windows, Linux Ctrl+Shift+X ) ). Installing the MinGW-w64 toolchain Get the latest version of MinGW-w64 via MSYS2 , which provides up-to-date native builds of GCC, MinGW-w64, and other helpful C++ tools and libraries. This will provide you with the necessary tools to compile your code, debug it, and configure it to work with IntelliSense .
To install the MinGW-w64 toolchain, check out this video or follow the steps below: You can download the latest installer from the MSYS2 page or use this direct link to the installer . Run the installer and follow the steps of the installation wizard. Note that MSYS2 requires 64 bit Windows 10 (version 1809 or later). In the wizard, choose your desired Installation Folder. Record this directory for later. In most cases, the recommended directory is acceptable.
The same applies when you get to setting the start menu shortcuts step. When complete, ensure the Run MSYS2 now box is checked and select Finish . This will open a MSYS2 terminal window for you. In this terminal, install the MinGW-w64 toolchain by running the following command: pacman -S --needed base-devel mingw-w64-ucrt-x86_64-toolchain Accept the default number of packages in the toolchain group by pressing Enter .
Enter Y when prompted whether to proceed with the installation. Add the path of your ucrt64 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 . In your User variables , select the Path variable and then select Edit . Select New and add the MinGW-w64 destination folder you recorded during the installation process to the list.
If you used the default settings above, then this will be the path: C:\msys64\ucrt64\bin . Select OK , and then select OK again in the Environment Variables window to update the PATH environment variable. You have to reopen any console windows for the updated PATH environment variable to be available. Check your MinGW installation To check that your MinGW-w64 tools are correctly installed and available, open a new Command Prompt and type: gcc --version g++ --version gdb --version You should see output that states which versions of GCC, g++ and GDB you have installed.
If this is not the case: Make sure your PATH variable entry matches the MinGW-w64 binary location where the toolchain was installed. If the compilers do not exist at that PATH entry, make sure you followed the previous instructions. If gcc has the correct output but not gdb , then you need to install the packages you are missing from the MinGW-w64 toolset. If on compilation you are getting the "The value of miDebuggerPath is invalid." message, one cause can be you are missing the mingw-w64-gdb package.
Create a Hello World app First, lets get a project set up. Launch a Windows command prompt (Enter Windows command prompt in the Windows search bar). Run the following commands. These will create an empty folder called projects where you can place all your VS Code projects. There, the next commands will create and navigate to a subfolder called helloworld . From there, you will open helloworld directly in VS Code. mkdir projects cd projects mkdir helloworld cd helloworld code .
The "code ." command opens VS Code in the current working folder, which becomes your "workspace". Accept the Workspace Trust dialog by selecting Yes, I trust the authors since this is a folder you created. As you go through the tutorial, you will see three files created in a .vscode folder in the workspace: tasks.json (build instructions) launch.json (debugger settings) c_cpp_properties.json (compiler path and IntelliSense settings) Add a Hello World source code file In the File Explorer title bar, select the New File button and name the file helloworld.cpp .
Add hello world source code Now paste in this source code: #include <iostream> #include <vector> #include <string> using namespace std ; int main () { vector<string> msg { "Hello" , "C++" , "World" , "from" , "VS Code" , "and the C++ extension!" }; for ( const string& word : msg) { cout << word << " " ; } cout << endl; } Now press ⌘S (Windows, Linux Ctrl+S ) to save the file. Notice how the file you just added appears in the File Explorer view ( ⇧⌘E (Windows, Linux Ctrl+Shift+E ) ) in the side bar of VS Code: You can also enable Auto Save to automatically save your file changes, by selecting File > Auto Save .
You can find out more about the other views in the VS Code User Interface documentation . Note : When you save or open a C++ file, you may see a notification from the C/C++ extension about the availability of an Insiders version, which lets you test new features and fixes. You can ignore this notification by selecting the X ( Clear Notification ). Explore IntelliSense IntelliSense is a tool to help you code faster and more efficiently by adding code editing features such as code completion, parameter info, quick info, and member lists.
To see IntelliSense in action, hover over vector or string to see their type information. If you type msg. in line 10, you can see a completion list of recommended member functions to call, all generated by IntelliSense: You can press the Tab key to insert a selected member. If you then add open parenthesis, IntelliSense will show information on which arguments are required. If IntelliSense is not already configured, open the Command Palette ( ⇧⌘P (Windows, Linux Ctrl+Shift+P ) ) and enter Select IntelliSense Configuration .
From the dropdown of compilers, select Use gcc.exe to configure. More information can be found in the IntelliSense configuration documentation . Run helloworld.cpp Remember, the C++ extension uses the C++ compiler you have installed on your machine to build your program. Make sure you have completed the "Installing the MinGW-w64 toolchain" step before attempting to run and debug helloworld.cpp in VS Code. Open helloworld.cpp so that it is the active file.
Press the play button in the top right corner of the editor. Choose C/C++: g++.exe build and debug active file from the list of detected compilers on your system. You'll only be asked to choose a compiler the first time you run helloworld.cpp . This compiler will be set as the "default" compiler in tasks.json file. After the build succeeds, your program's output will appear in the integrated Terminal . Congratulations!
You've just run your first C++ program in VS Code! Understanding tasks.json The first time you run your program, the C++ extension creates a tasks.json file, which you'll find in your project's .vscode folder. tasks.json stores your build configurations. Your new tasks.json file should look similar to the JSON below: { "tasks" : [ { "type" : "cppbuild" , "label" : "C/C++: g++.exe build active file" , "command" : "C: \\ msys64 \\ ucrt64 \\ bin \\ g++.exe" , "a
The rest of this page is on the live site.