MSYS2 UCRT64 and VS Code Setup Guide - GitMasterNikanjam/C_WiKi GitHub Wiki

MSYS2 UCRT64 and VS Code Setup Guide

This guide explains how to install a general-purpose native Windows C/C++ toolchain with MSYS2 UCRT64, GCC, CMake, and Ninja, and how to configure the integrated terminal and C/C++ tools in Visual Studio Code.

Scope: UCRT64 builds native Windows applications with GCC. It is not an STM32 cross-compiler. To build STM32 firmware, install an ARM embedded toolchain such as arm-none-eabi-gcc and configure the relevant embedded project separately.

1. Install MSYS2

  1. Download and run the Windows installer from the official MSYS2 website.

  2. Keep the default installation directory, C:\msys64, unless you have a specific reason to use another location.

  3. At the end of setup, leave the option to launch MSYS2 enabled. Open the MSYS2 UCRT64 terminal.

  4. Update MSYS2 and its package databases from that terminal:

    pacman -Suy
    

    If the update asks you to close the terminal, close it, reopen MSYS2 UCRT64 from the Start menu, and run pacman -Suy again to finish the update. MSYS2 supports full system upgrades; avoid partial upgrades. See the official MSYS2 update guide.

2. Install GCC, CMake, and Ninja

In the MSYS2 UCRT64 terminal, install the compiler and build tools:

pacman -S --needed mingw-w64-ucrt-x86_64-gcc mingw-w64-ucrt-x86_64-cmake mingw-w64-ucrt-x86_64-ninja

This installs GCC/G++, CMake, and Ninja for the UCRT64 environment. To use GDB for debugging, install it as well:

pacman -S --needed mingw-w64-ucrt-x86_64-gdb

MSYS2 manages packages with pacman. UCRT64 packages use the mingw-w64-ucrt-x86_64- prefix. See the MSYS2 environment guide and the GCC UCRT64 package page.

Verify the installation

These commands should show the UCRT64 environment, tool paths, and installed versions:

echo "$MSYSTEM"
which gcc
which g++
which cmake
which ninja
gcc --version
g++ --version
cmake --version
ninja --version

echo "$MSYSTEM" should print UCRT64. Tool paths usually start with /ucrt64/bin/. The UCRT64 environment sets PATH so its tools are available automatically. See MSYS2 environments.

3. Install VS Code extensions

In VS Code, install Microsoft's C/C++ extension (ms-vscode.cpptools). Microsoft's CMake Tools extension (ms-vscode.cmake-tools) is useful for configuring and building CMake projects from the editor.

VS Code extensions do not install GCC, CMake, or Ninja; install those tools in MSYS2. The C/C++ extension uses compilerPath to discover standard headers and configure IntelliSense. See the VS Code C/C++ documentation and C/C++ settings reference.

4. Add the UCRT64 terminal profile to VS Code

  1. Press Ctrl+Shift+P.
  2. Run Preferences: Open User Settings (JSON).
  3. Add the following entries to the existing JSON object. Do not replace the whole file with this snippet. If terminal.integrated.profiles.windows already exists, merge the new profile into that object.
"terminal.integrated.profiles.windows": {
    "MSYS2 UCRT64": {
        "path": "C:\\msys64\\usr\\bin\\bash.exe",
        "args": ["--login", "-i"],
        "env": {
            "MSYSTEM": "UCRT64",
            "CHERE_INVOKING": "1"
        }
    }
},
"terminal.integrated.defaultProfile.windows": "MSYS2 UCRT64"

CHERE_INVOKING tells the login shell to keep VS Code's current working directory instead of changing to the home directory. The path above assumes the default MSYS2 installation directory, C:\msys64; change it if you installed MSYS2 elsewhere. See VS Code terminal profiles.

Open a new integrated terminal after saving the settings. If you do not want UCRT64 as the default, remove the defaultProfile setting and choose MSYS2 UCRT64 from the menu next to New Terminal.

5. Configure IntelliSense for GCC

For workspace-specific IntelliSense settings, run C/C++: Edit Configurations (JSON) from the Command Palette (Ctrl+Shift+P). In .vscode/c_cpp_properties.json, set compilerPath to the UCRT64 G++ executable:

{
    "configurations": [
        {
            "name": "UCRT64 GCC",
            "includePath": ["${workspaceFolder}/**"],
            "defines": [],
            "compilerPath": "C:/msys64/ucrt64/bin/g++.exe",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "windows-gcc-x64"
        }
    ],
    "version": 4
}

If .vscode/c_cpp_properties.json already has configurations, add or update compilerPath and intelliSenseMode in the configuration you use. Make sure the compiler path matches your installation. This setting configures IntelliSense; it does not configure or run CMake by itself. See the official VS Code GCC/MinGW guide.

6. Configure and build a CMake project

Open the project root in the MSYS2 UCRT64 terminal. Specify Ninja explicitly to avoid CMake selecting an unrelated generator such as NMake or a Visual Studio generator:

cmake -S . -B build/ucrt64 -G Ninja -DCMAKE_BUILD_TYPE=Debug
cmake --build build/ucrt64

For a Release build:

cmake -S . -B build/ucrt64-release -G Ninja -DCMAKE_BUILD_TYPE=Release
cmake --build build/ucrt64-release

MSYS2 recommends using its MinGW/UCRT64 CMake and Ninja packages for native Windows projects and selecting the generator explicitly. See Using CMake in MSYS2.

If you use CMake Tools, open the project in VS Code, run CMake: Select a Kit, and select the UCRT64 GCC kit. Then run CMake: Select a Generator, choose Ninja, and run CMake: Configure. The terminal workflow above works without the CMake Tools extension.

CMake cache note

CMake stores the selected compiler and generator in each build directory. If that directory was previously configured with NMake or another compiler, use a fresh build directory such as build/ucrt64-ninja, or clear the cache only after confirming its location.

7. Troubleshooting

UCRT64 does not appear in the VS Code terminal list

  • Confirm that C:\msys64\usr\bin\bash.exe exists.
  • Check the JSON syntax; entries in an object need commas between them.
  • Run Terminal: Select Default Profile from the Command Palette, or restart VS Code.
  • If MSYS2 is installed elsewhere, update the profile's path value.

GCC or CMake cannot be found

Run these commands in the VS Code terminal:

echo "$MSYSTEM"
which g++
which cmake
which ninja

If MSYSTEM is not UCRT64, select the UCRT64 terminal profile. If a command is missing, install its package from the MSYS2 UCRT64 terminal with pacman.

CMake selects NMake or the wrong compiler

Configure the project from the UCRT64 terminal and pass -G Ninja. If the build directory already has a different generator or compiler cached, select a new build directory.

Official references