Compile Run and Debug C Program in WSL
I use below softwares for C development in Windows 10.
- VS Code
- WSL 2 (Ubuntu)
Install development tool
sudo apt-get update
sudo apt install gcc
sudo apt-get install build-essential gdb
sudo apt-get install make
or
sudo apt-get install build-essential gcc g++ gdb make
Configure VS Code
In VS Code, press Ctrl+Shift+P
, type C/C++
and select Edit Configurations (UI)
. Set Compiler path to /usr/bin/gcc
and IntelliSense mode to gcc-x64
.
Open helloworld/.vscode/c_cpp_properties.json
, the file should look like this
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
Setup tasks.json
Paste following codes in tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "build on WSL",
"command": "gcc",
"args": [
"-g",
"-o",
"./${fileBasenameNoExtension}.out",
"'${relativeFileDirname}/${fileBasename}'"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": ["$gcc"],
"detail": "gcc build task"
},
{
"label": "run on WSL",
"type": "shell",
"command": "./${fileBasenameNoExtension}.out",
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": [
"$gcc"
],
"detail": "gcc run task"
}
]
}
Setup launch.json
Paste following codes in launch.json
{
// 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": "${workspaceFolder}/${fileBasenameNoExtension}.out",
"args": [],
"stopAtEntry": true,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
},
{
"name": "C/C++: g++ build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.out",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
//"preLaunchTask": "C/C++: g++ build active file"
"preLaunchTask": "build on WSL"
},
]
}
Build Your Code
Press ctrl+shift+b
Debug Your Code
Press F5
Build and Run Your Code
Press Ctrl + F5
=> **NOT WORK((