C/C++ development on Windows in VS Code and WSL2 #1: terminal apps

There is a lot of toolchains for C/C++. Although there are IDEs, some developers prefer using text editors and manual compilation. On Windows you can use VS Code and so called Windows Subsystem for Linux to compile like on native Linux system.

Installation

VS Code installation

  1. Download an installer from the official website.
  2. Install.

Yes, it’s as simple as that.

VS Code installation.

WSL2 Installation

This step is a little bit harder, you can follow official instructions here. As for the distribution, I use Ubuntu. Please note that this will install the Hyper-V, so if you use VirtualBox, you won’t be able to use VT-x/AMD-v feature.

GNU compiler and debugger install

After the successful installation of WSL2 it’s time to install build tools. On Ubuntu, all required packages can be installed with:

sudo apt update
sudo apt install build-essential

Now verify the installation:

gcc --version
g++ --version
gcc/g++ install verification.

Configuration

Create a project directory via bash:

mkdir folder_name

or via the GUI:

explorer.exe .

Change to the newly created directory and open VS Code:

cd folder_name
code .

Do not forget the .

You should see WSL: (distro name) in the bottom left corner of the VS Code.

Now it’s time to create a simple Hello World program to test the build tasks:

#include <stdio.h>
int main(){
    printf("Hello World!\n");
    return 0;
}
  1. Click on the Terminal and Configure Default Build Task in the title.
  2. Select C/C++ gcc build active file.
  3. Open the previously created program.
  4. Press Ctrl+Shift+B to run the task, gcc output will be in the terminal.
  5. Click on the Terminal and New Terminal.
  6. Run the compiled program by ./program_name.

The task can be further edited, it’s saved in the .vscode/tasks.json file. Usually you’d want to change command to /usr/bin/g++ and add or replace some arguments in args.

VS Code with opened tasks configuration.

Notes

This configuration is great for simple projects. Because VS Code is not an IDE, it does not store any additional ‘project’ files except the configuration (e.g. tasks.json). You can always run the build manually by opening the terminal and running the compiler.

For more complicated projects it’s handy to use the Makefile. This technique will be discussed in the next chapter.