How To Setup A C Project With WSL2 And Visual Studio Code – A Short Overview

In this post, I will show you guys how I set up a C Compiler on my Windows 11 machine. Since as a tutor I will check completed tasks written in C, but I personally want to avoid a big Visual Studio solution for it, I will use the WSL2 on my machine to compile C and the Visual Studio Code IDE for debugging and code checks (also, IntelliSense).

What is WSL?

“Windows Subsystem for Linux” is a working UNIX environment on a Windows distribution. While WSL1 inherits a “kernel emulator”, WSL2 supports a full working linux kernel underlying, making WSL a performant Unix sandbox on Windows. At the end of this article there are some links for setup guidance.

I strongly recommend WSL2. It is way more performant in comparison and now common practice.

I strongly recommend installing Ubuntu, since it is the one of the most stable and user-friendly solutions as a WSL.

Prequisite: Windows 10 1909 (I guess. It’s availiable for quite some time now.)

I strongly recommend installing Windows Terminal (Preview) over the Microsoft Store.

What is Visual Studio Code?

Unlike Visual Studio, Visual Studio Code is a light-weight IDE with (AFAIK) no compiler solutions embedded or whatsoever. It can be used to compile almost any standard programming language, if that’s what the user wants.

In this case, I will use Visual Studio Code to compile and debug C Projects. Using the WSL for it will need one extra setup step, but thats fine by me.

In Visual Studio Code, there are Extensions installable via Ctrl+Shift+X. Choose the WSL Extension. What it does is to set up an instance of VSCode inside of the WSL of your choice.

The best feature about VSCode is IntelliSense. The auto-completion for coding. No need to look up the functionalities in some sort of documentation. This is by far the best feature. Otherwise, Sublime Text 4 is also highly customizable.

I need a compiler!

Yes, that’s what the WSL2 is for. Open your Ubuntu distribution (either in shell, in VSCode terminal or in Windows Terminal preview) and type in the following:

sudo apt install g++ gdb

And there you have it! That’s your working compiler now! Set up the VSCode configuration for a C Project, and then, you should be ready to go.

Keep in mind, that this is a short-term introduction. For any questions, please feel free to contact me by commenting on this blog page or sending a pm.
Thanks and stay safe!

Links

Most important one:
https://code.visualstudio.com/docs/cpp/config-wsl

https://www.windowscentral.com/how-install-wsl2-windows-10
https://docs.microsoft.com/de-de/windows/wsl/setup/environment
https://code.visualstudio.com/

Author

  • Benjamin Coban

    Hi there, Benjamin here! Interested in theoretical computer science, DevOps and being a tiny gem of musical expression with caffine addiction, I try to reach the public with posts about theory, applications and their interaction. Feel free to stop by and start a discussion! 🙂

    View all posts