Compiling a C++ source file in Visual Studio Code with MinGW

I followed this instruction and configured C/C++ extension as follows:

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.19041.0",
            "compilerPath": "D:\\dev\\tools\\x86_64-12.2.0-release-win32-seh-rt_v10-rev0\\mingw64\\bin\\g++.exe",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "windows-gcc-x64"
        }
    ],
    "version": 4
}

then I created and successfully compiled helloworld.cpp:

#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;
}

I also was able to run the program I compiled by pressing triangle button at top-right corner, and IntelliSense also worked:

tasks.json was created automatically:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "D:\\dev\\tools\\x86_64-12.2.0-release-win32-seh-rt_v10-rev0\\mingw64\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "D:\\dev\\tools\\x86_64-12.2.0-release-win32-seh-rt_v10-rev0\\mingw64\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

My project tree looked like this at this point:

Then I set a breakpoint and it was triggered successfully when I started debug session:

Alternatively we use commands:

My plugin version:

6 Responses to Compiling a C++ source file in Visual Studio Code with MinGW

  1. dmitriano says:

    Using GCC with MinGW:
    https://code.visualstudio.com/docs/cpp/config-mingw

    Debug C++ in Visual Studio Code:
    Windows debugging with GDB (“miDebuggerPath”: “c:\\mingw\\bin\\gdb.exe”)
    https://code.visualstudio.com/docs/cpp/cpp-debug

    Get started with CMake Tools on Linux:
    https://code.visualstudio.com/docs/cpp/CMake-linux

    Visual Studio Code CMake Tools ignores custom kits?
    https://stackoverflow.com/questions/73915729/visual-studio-code-cmake-tools-ignores-custom-kits

  2. dmitriano says:

    How to configure VS Code’s CMake Tools Extension for GCC and MSYS Makefiles on Windows?
    https://stackoverflow.com/questions/71000530/how-to-configure-vs-codes-cmake-tools-extension-for-gcc-and-msys-makefiles-on-w

  3. dmitriano says:

    https://stackoverflow.com/questions/3081815/c-errors-while-compiling
    If you build the above code snippet with gcc you get the errors you mention. If you use g++ it build ok, this makes sense since g++ will make sure all the proper stuff it put together when build C++.

  4. dmitriano says:

    Tried:
    set PATH=C:\WINDOWS\system32;C:\WINDOWS
    set PATH=%PATH%;”D:\dev\tools\mingw64\bin”
    and then run VS Code, but the kits still not found.

Leave a Reply

Your email address will not be published. Required fields are marked *