Install WSL & Code C in VS Code on Windows 10/11
Summary:
Enable Windows Subsystems for Linux (WSL) for Windows 10/11 on PC.
Install the WSL extension on VS Code.
Connect to WSL on VS Code.
Access the Linux directory on File Explorer.
Run C code on Microsoft Visual Code Studio (VS Code) through WSL.
Install WSL:
Search Command Prompt (cmd) and open it.
wsl --install
WSL will install in a few seconds as noted in cmd.
Ubuntu:
Once WSL is installed, search Ubuntu and open it.
You will be prompted to create a username and password for Ubuntu on the terminal. (The password will be invisible on the terminal).
Once you have created your credentials, enter each of the codes into the Ubuntu terminal.
sudo apt update
sudo apt install build-essential
Verify the version using the code below to see if it is installed.
gcc --version
Linux Folder in File Explorer:
Open File Explorer and go to the following directory below.
Linux>Ubuntu>home
Open your username folder.
Create a folder to save your C scripts.
WSL Extension on VS Code:
Search VS Code and open it.
On the bottom left corner of VS Code, click the button with double arrows (Open a Remote Window).
Connect to WSL
Make sure the following extensions are installed on WSL: UBUNTU - INSTALLED.
File>Open Folder (Go to the folder directory you previously created).
File>New File
Name the file with .c extension (Ex. test.c).
Run C Script on VS Code With WSL:
Let's run the "Hello World" C script on VS Code.
Paste the code below.
#include <stdio.h>
int main() {
printf("Hello World\n");
return 0;
}
Terminal>New Terminal
Compile the code in the terminal by entering the code below.
gcc -std=c89 -o t1 test.c
Run it with the code below.
./t1
A "Hello World" message will appear on the terminal.