Set up C/C++ with MSYS2 in Cursor

Hello There! :slight_smile:

  • First, search and install C/C++ extension from Anysphere (cursor.com) in the Cursor marketplace.

  • Search for and install the clangd extension from llvm-vs-code-extensions (llvm.org).

  • Second, install and set up MSYS2.

  1. From MSYS2 Website, download the required version for your device (x86 or ARM64)

  2. Install the application by clicking Next for all steps (leave the recommended path as C:\msys64).

  3. Run the following command in the terminal that opens.

    (If it is not open, search for “MSYS2 UCRT64” in the Windows search bar and open it.)

pacman -S --needed base-devel mingw-w64-ucrt-x86_64-toolchain

Accept the default by pressing Enter.

Then enter Y when prompted whether to proceed with the installation.

  1. Add the path of your MinGW-w64 bin folder to the Windows PATH environment variable.

    1. In windows search bar Search for Edit environment variables for your account.
    2. In your User variables, select the Path variable and then select Edit.
    3. Click New and add the MinGW-w64 destination folder you selected during installation.
      If you used the default settings, the path will beC:\msys64\ucrt64\bin.
    4. Select OK, and then select OK again in the Environment Variables window to update the PATH environment variable.
  2. Check your MinGW installation

  • Open Command Prompt (CMD) and run:
gcc --version
g++ --version
gdb --version
  • You should see version information displayed.

If your terminal cannot find any of these commands, run the appropriate command in MSYS2 UCRT64 (after reopening it):

# Use this command and check after in cmd.
pacman -S --needed base-devel mingw-w64-ucrt-x86_64-toolchain

#If the problem persists.

# try this for gdb
pacman -S mingw-w64-ucrt-x86_64-gdb

#try this for gcc and g++
pacman -S mingw-w64-ucrt-x86_64-gcc

  1. Install Clang toolchain

  • In MSYS2 UCRT64, run:
pacman -S mingw-w64-ucrt-x86_64-clang

pacman -S mingw-w64-ucrt-x86_64-clang-tools-extra

Enter Y when prompted whether to proceed with the installation.

  • Finally, in Cursor

  1. Create a project folder.
  2. Inside the project folder, create a folder named .vscode
  3. inside .vscode folder create 3 files launch.json | tasks.json | settings.json
  4. Copy the code below and paste it into your correct files.

launch.json

{
    "version": "0.2.0",
    "configurations": [
      {
        "name": "Debug (MSYS2 UCRT64)",
        "type": "cppdbg",
        "request": "launch",
        "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${fileDirname}",
        "environment": [],
        "externalConsole": false,
        "MIMode": "gdb",
        "miDebuggerPath": "C:/msys64/ucrt64/bin/gdb.exe",
        "preLaunchTask": "Build with MSYS2 Clang (UCRT64)",
        "setupCommands": [
          {
            "description": "Enable pretty-printing",
            "text": "-enable-pretty-printing",
            "ignoreFailures": true
          }
        ]
      }
    ]
  }

tasks.json

{
    "version": "2.0.0",
    "tasks": [
      {
        "label": "Build with MSYS2 Clang (UCRT64)",
        "type": "shell",
        "command": "C:/msys64/ucrt64/bin/clang++.exe",
        "args": [
          "-g",
          "-std=c++20",
          "${file}",
          "-o",
          "${fileDirname}/${fileBasenameNoExtension}.exe"
        ],
        "options": {
          "cwd": "${fileDirname}"
        },
        "problemMatcher": ["$gcc"],
        "group": {
          "kind": "build",
          "isDefault": true
        }
      }
    ]
  }

settings.json

{
    "clangd.path": "C:/msys64/ucrt64/bin/clangd.exe",
    "clangd.arguments": [
      "--background-index",
      "--clang-tidy",
      "--completion-style=detailed",
      "--header-insertion=iwyu"
    ],
  
  
    "terminal.integrated.env.windows": {
      "PATH": "C:/msys64/ucrt64/bin;${env:PATH}"
    }
  }

Now create your C/C++ files and Enjoy!:wink:

1 Like