iTranslated by AI
Setting up a C Development Environment with VS Code & WSL 2
WSL 2 (Windows Subsystem for Linux) allows us to set up an environment where you can easily compile and debug C language code using the Tasks feature of VS Code on an installed Ubuntu distribution.
Target Audience
- People who want to write C code in VS Code
- People who are already writing C code in another environment but are considering migrating to an environment with VS Code and WSL 2
In both cases, this guide assumes a scenario where a high-end (?) development environment is not necessarily required—for example, for those who "only write C code for university classes."
Setup Steps
1. Install VS Code
We will use "VS Code" as the editor for writing C code. Click "Download for Windows Stable Build" on the official website to download the installer.

After downloading, simply run the installer and follow the on-screen instructions to complete the VS Code installation.
2. Install Ubuntu via WSL 2
Next, let's install Ubuntu on WSL 2. Launch PowerShell (or Command Prompt) and run the following command:
wsl --install -d Ubuntu
Wait patiently, and an Ubuntu window should eventually appear.

Follow the instructions in the Ubuntu window to set your username and password. Once you reach the state shown in the image below, the setup for WSL 2 and Ubuntu is complete.

3. Install Extensions in VS Code
To use VS Code with Ubuntu on WSL 2, "Remote Development" is essential. Click the "Install" button to install it.

Once the installation is successful, you should see a screen like the one below. To prepare for installing the "C/C++ Extension Pack" described later, click the "Open the Menu" button.

Select and click "New: WSL Window". A new VS Code window will open.

Next, add the "C/C++ Extension Pack" from the WSL-side window you just opened[1]. Just click "Install in WSL: Ubuntu" as before.

It will take about 5 minutes for this extension to be added. Please wait patiently. After a while, it is successfully completed if it looks like the image below.

4. Install GCC and GDB on Ubuntu
The VS Code Tasks feature will not work and will result in an error if both GCC (compiler) and GDB (debugger) are not available on Ubuntu via WSL 2. Therefore, run the following command to install both. If you are prompted for a password, enter the user password you set in Ubuntu on WSL 2.
sudo apt install gcc gdb
5. Create a Workspace and Run C Language Code
Create a workspace (working directory) for writing C code in VS Code. In this example, we will create a directory named example directly under the home directory and use it as the workspace. Specifically, just run the following command as is.
cd && mkdir test && code $_
Then, VS Code will launch with example as the workspace. If a screen like the one below appears, click the blue button.[2]

Next, create the following program example.c. This is a simple program that displays Hello, world! on the screen when compiled and executed. Feel free to rewrite the code as you like.[3]
#include <stdio.h>
int main(void)
{
printf("Hello, world!\n");
return 0;
}
With example.c open, press the F5 key. From the menu that appears, select "C/C++: gcc build and debug active file" (the bottom option in the image below).

Then, example.c will be compiled, and the generated binary file example will be executed automatically. This is the VS Code Tasks feature. It's very convenient because it saves you the trouble of typing commands manually!

However, as shown in the image above, the execution result is not displayed, so it's hard to tell if it worked correctly. To see the execution result, click the "Terminal" tab.

It clearly shows Hello, world!. Once you've confirmed this, the entire setup process is successfully complete!
(Supplemental) Customizing tasks.json
In Step 5, compiling example.c with the Tasks feature generated a binary file named example without an extension. Usually, when compiling with GCC, the generated binary file is given a .out extension. With the Tasks feature, you can achieve the same by editing .vscode/tasks.json[4], which is automatically generated in the root of the workspace.
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc build active file",
"command": "/usr/bin/gcc",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
- "${fileDirname}/${fileBasenameNoExtension}"
+ "${fileDirname}/${fileBasenameNoExtension}.out"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
After modifying it as shown above, when you compile example.c with the Tasks feature again, you will see that example.out is generated instead.

-
The window showing "WSL: Ubuntu" in the bottom-left status bar. ↩︎
-
VS Code includes a feature called "Restrict Mode" that disables extensions and debugging by default for security purposes. Since the workspace created in this article was made by the user themselves and requires extension and debugging functionality, we are disabling Restrict Mode (setting it to trusted). ↩︎
-
For example, you might want to change it to code that displays approximate values of pi (
) or Napier's constant (\pi )! ↩︎\mathrm{e} -
The configuration file for the Tasks feature. ↩︎
Discussion