Requirements

  • MSYS2 installed and completed initialization
  • VS Code installed

MSYS2

MSYS2 is a collection of tools and libraries providing you with an easy-to-use environment for building, installing and running native Windows software.

MinGW-w64 Toolchain

Execute in MSYS2 Shell:

$ pacman -S --needed base-devel
// (√)x86_64:
$ pacman -S mingw-w64-x86_64-toolchain
// i686:
$ pacman -S mingw-w64-i686-toolchain

And then check the gcc version info in MinGW64 Shell:

$ gcc -v
Using built-in specs.
COLLECT_GCC=C:\msys2\mingw64\bin\gcc.exe
COLLECT_LTO_WRAPPER=C:/msys2/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../gcc-11.2.0/configure --prefix=/mingw64 --with-local-prefix=/mingw64/local --build=x86_64-w64-mingw32 --host=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --with-native-system-header-dir=/mingw64/x86_64-w64-mingw32/include --libexecdir=/mingw64/lib --enable-bootstrap --enable-checking=release --with-arch=x86-64 --with-tune=generic --enable-languages=c,lto,c++,fortran,ada,objc,obj-c++,jit --enable-shared --enable-static --enable-libatomic --enable-threads=posix --enable-graphite --enable-fully-dynamic-string --enable-libstdcxx-filesystem-ts --enable-libstdcxx-time --disable-libstdcxx-pch --disable-libstdcxx-debug --enable-lto --enable-libgomp --disable-multilib --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-libiconv --with-system-zlib --with-gmp=/mingw64 --with-mpfr=/mingw64 --with-mpc=/mingw64 --with-isl=/mingw64 --with-pkgversion='Rev5, Built by MSYS2 project' --with-bugurl=https://github.com/msys2/MINGW-packages/issues --with-gnu-as --with-gnu-ld --with-boot-ldflags='-pipe -Wl,--dynamicbase,--high-entropy-va,--nxcompat,--default-image-base-high -Wl,--disable-dynamicbase -static-libstdc++ -static-libgcc' 'LDFLAGS_FOR_TARGET=-pipe -Wl,--dynamicbase,--high-entropy-va,--nxcompat,--default-image-base-high' --enable-linker-plugin-flags='LDFLAGS=-static-libstdc++\ -static-libgcc\ -pipe\ -Wl,--dynamicbase,--high-entropy-va,--nxcompat,--default-image-base-high\ -Wl,--stack,12582912'
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.2.0 (Rev5, Built by MSYS2 project)

Include Path

Save/copy paths between ’#include <…> search starts here:’ and ‘End of search list.’.

C++: gcc -v -x c++ -E /dev/null

C:/msys2/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../include/c++/11.2.0
C:/msys2/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../include/c++/11.2.0/x86_64-w64-mingw32
C:/msys2/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../include/c++/11.2.0/backward
C:/msys2/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/include
C:/msys2/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../include
C:/msys2/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/include-fixed
C:/msys2/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/include

C: gcc -v -x c -E /dev/null

C:/msys2/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/include
C:/msys2/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../include
C:/msys2/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/include-fixed
C:/msys2/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/include

Environment Variables

DO NOT set $USERPROFILE as $HOME will break other programs(e.g. git-for-windows, openssh, etc.). For consistency, <MSYS2>\mingw64\bin will NOT be added to $PATH either.

VS Code

Install C/C++ Extension, then follow the steps below.

Configure extension according to the directory structure of demo project:

.
├── .vscode                     · vscode workspace settings
│   ├── c_cpp_properties.json   · c/cpp prop.
│   ├── launch.json             · debug
│   └── tasks.json              · build task
├── src                         · source codes
│   └── main.cpp
└── bin                         · binary output
    └── main.exe

Extension Configurations

c_cpp_properties.json

For C: Replace includePath with C includepaths, and set compilerPath as gcc.exe.

{
    "configurations": [
        {
            "name": "mingw-w64-x86_64-gcc",
            "includePath": [
                "${workspaceFolder}/**",
                "C:/msys2/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../include/c++/10.3.0",
                "C:/msys2/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../include/c++/10.3.0/x86_64-w64-mingw32",
                "C:/msys2/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../include/c++/10.3.0/backward",
                "C:/msys2/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/include",
                "C:/msys2/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../include",
                "C:/msys2/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/include-fixed",
                "C:/msys2/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.19041.0",
            "compilerPath": "C:/msys2/mingw64/bin/g++.exe",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "windows-gcc-x64"
        }
    ],
    "version": 4
}

launch.json

preLaunchTask needs to be consistent with label field in tasks.json.

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++.exe - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileWorkspaceFolder}\\bin\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "C:/msys2/mingw64/bin",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\msys2\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe build active file"
        }
    ]
}

tasks.json

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "C:/msys2/mingw64/bin/g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileWorkspaceFolder}\\bin\\${fileBasenameNoExtension}.exe",
            ],
            "options": {
                "cwd": "C:/msys2/mingw64/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

.\src\main.cpp:

#include <iostream>
using namespace std;

int main()
{
    cout << "--- hello ---";
    return 0;
}

Test

Run Build Task(CTRL+SHIFT+B):

> Executing task: C/C++: g++.exe build active file <

Starting build...
C:/msys2/mingw64/bin/g++.exe -g C:\Users\HUMOR\Desktop\c_cpp\src\main.cpp -o C:\Users\HUMOR\Desktop\c_cpp\bin\main.exe
Build finished successfully.

Start Debugging(F5):

>  & 'c:\Users\HUMOR\.vscode\extensions\ms-vscode.cpptools-1.7.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-r1nmqk1x.mud' '--stdout=Microsoft-MIEngine-Out-c440ha2q.azn' '--stderr=Microsoft-MIEngine-Error-o1qczzfj.1dz' '--pid=Microsoft-MIEngine-Pid-vgmz3pnw.p52' '--dbgExe=C:\msys2\mingw64\bin\gdb.exe' '--interpreter=mi'
--- hello ---

Run Without Debugging(CTRL+F5):

>  & 'c:\Users\HUMOR\.vscode\extensions\ms-vscode.cpptools-1.7.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-5fcfpycg.2hx' '--stdout=Microsoft-MIEngine-Out-nnudexwd.kuo' '--stderr=Microsoft-MIEngine-Error-owqt32o4.s4r' '--pid=Microsoft-MIEngine-Pid-2gm0d35o.zwf' '--dbgExe=C:\msys2\mingw64\bin\gdb.exe' '--interpreter=mi' 
--- hello ---

References