How to Set Up C Programming in VS Code on Windows 11 and Linux (GCC & MinGW)

Avatar
H

Henrik Lindqvist

Infrastructure & Linux SRE••
1831Views
5mRead
0Reactions

Setting up Visual Studio Code for C programming often frustrates beginners with missing include paths,'gcc' is not recognized errors, or terminal consoles that close before output can be read.

VS Code is a lightweight editor, not a full IDE. To compile and debug C code, you need:

  1. A C compiler toolchain (GCC & GDB).

  2. The Microsoft C/C++ Extension for IntelliSense.

  3. Configured.vscode/tasks.json (for building) and.vscode/launch.json (for debugging).

Here is the exact setup for Windows 11 and Linux.


1. Install the GCC Toolchain

On Windows 11:

Install MinGW-w64 using the modern w64devkit standalone distribution or MSYS2:

  1. Download w64devkit from its official GitHub releases.

  2. Extract the folder to C:\w64devkit.

  3. Open Windows System Environment Variables$\rightarrow$Path$\rightarrow$ Click New$\rightarrow$ Add C:\w64devkit\bin.

  4. Open PowerShell and verify:

gcc --version
gdb --version

On Ubuntu / Debian Linux:

Run:

sudo apt update
sudo apt install build-essential gdb -y
gcc --version

2. Install VS Code Extensions

Open VS Code (Ctrl + Shift + X) and install:

  • C/C++(by Microsoft) – Provides syntax highlighting, auto-complete, and debugging integration.

  • Code Runner (optional, for 1-click execution).


3. Create a Project & Sample C Program

Create a new directory and file main.c:

#include <stdio.h>

int main() {
    char name[50];
    printf("Enter your name: ");
    if (fgets(name, sizeof(name), stdin) != NULL) {
        printf("Hello, %s Welcome to C programming in VS Code!\n", name);
    }
    return 0;
}

4. Configure Automated Build Task (.vscode/tasks.json)

Press Ctrl + Shift + P$\rightarrow$ select Tasks: Configure Default Build Task$\rightarrow$ select C/C++: gcc build active file.

Ensure your.vscode/tasks.json matches:

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

Press Ctrl + Shift + B anytime to compile your code into an executable.


5. Configure GDB Debugging (.vscode/launch.json)

Press F5 or create.vscode/launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C/C++: Debug with GDB",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: gcc build active file"
        }
    ]
}

Troubleshooting Common Gotchas

  • 'gcc' is not recognized as an internal or external command: VS Code was open before adding GCC to your Windows PATH. Close and restart VS Code completely.

  • Terminal doesn't accept input in Code Runner: Go to Settings (Ctrl + ,) $\rightarrow$ Search runInTerminal$\rightarrow$ Check Code-runner: Run In Terminal.

  • Squiggly red lines under#include <stdio.h>: Press Ctrl + Shift + P$\rightarrow$C/C++: Edit Configurations (UI)$\rightarrow$ Set Compiler path to C:/w64devkit/bin/gcc.exe.

Connect with KoderSolution 🚀

Master modern software engineering, scalable architectures, and advanced backend systems.

What's your setup?What's your production setup for this scenario? Have you encountered similar architectural bottlenecks or edge-case gotchas?
Henrik Lindqvist
H
Written by•Infrastructure & Linux SRE

Henrik Lindqvist

Runs small, hardened Docker containers and Alpine builds. Likes automated backups, Terraform scripts, and keeping server configs reproducible.

React to this article

Recommended Articles

View all articles