Skip to content

Instantly share code, notes, and snippets.

@notheotherben
Last active January 21, 2021 16:19
Show Gist options
  • Save notheotherben/0e9f9d296db6eb7f76a5284214a279b8 to your computer and use it in GitHub Desktop.
Save notheotherben/0e9f9d296db6eb7f76a5284214a279b8 to your computer and use it in GitHub Desktop.
VSCode GCC+WSL Configuration

Build and debug C in VSCode on Windows Subsystem for Linux

This set of launch.json and task.json files allow you to build and debug individual C files from within Windows while using the gcc collection installed within your WSL environment.

Steps

Ensure you have GCC+GDB installed in WSL

sudo apt-get update
sudo apt-get install -y gcc gdb

Add the launch.json and tasks.json files to your project

Exactly what is says on the tin...

Open a C program's main() file and press F5

This will run the Build (GCC) build task, which can be manually triggered with Ctrl+Shift+B and will then launch the compiled binary within WSL, attaching a gdb debugger to it.

{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": ".",
"environment": [],
"externalConsole": true,
"pipeTransport": {
"debuggerPath": "/usr/bin/gdb",
"pipeProgram": "${env:windir}\\sysnative\\bash.exe",
"pipeArgs": [
"-c"
],
"pipeCwd": "${fileDirname}"
},
"logging": {
"engineLogging": true,
"trace": true
},
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"targetArchitecture": "x64",
"preLaunchTask": "Build (GCC)",
"sourceFileMap": {
"/mnt/c": "c:\\",
"/mnt/d": "d:\\"
}
},
]
}
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Build (GCC)",
"command": "gcc",
"type": "shell",
"args": [
"-o",
"$(wslpath '${fileDirname}/${fileBasenameNoExtension}')",
"-g",
"$(wslpath '${file}')"
],
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"options": {
"shell": {
"executable": "${env:windir}\\sysnative\\bash.exe",
"args": [
"-c"
]
}
}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment