Using valgrind on Windows in CLion (with WSL)

Jetbrains CLion is an amazing tool to write C / C++ code but it can be a bit tricky to use valgrind on Windows. Therefore, I am gonna explain how to install Windows Subsystem for Linux (WSL Debian), how to install CMake on Debian, and how to link WSL to CLion on Windows.

Install Windows Subsystem for Linux (Windows 10)

In order to install Windows Subsystem for Linux on Windows 10, you must first enable WSL by opening the PowerShell as Administrator and running:

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

Then, to enable the Virtual Machine feature you must run:

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

In order to enable the new features, you must now restart your computer.

You are now ready to install Linux. First, you must download and install:

https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi

Then, you must set WSL 2 as the default version when installing a new Linux distribution. To do so, you must type the following in the PowerShell:

wsl --set-default-version 2

Then, you can download and install the Debian distribution:

Once the Debian distribution is installed, you should see a Debian icon in the start menu. You must run this app and then enter a username associated with a password.

Install CMake

In order to properly install CMake you will need the following packages:

sudo apt-get update
sudo apt-get install openssh-server wget gcc clang gdb build-essential libssl-dev

Then you must download the CMake version that you want (curently the latest CMake version supported by CLion is 3.17.5).

You can check the latest versions of cmake at https://cmake.org/download/.

wget https://github.com/Kitware/CMake/releases/download/v3.17.5/cmake-3.17.5.tar.gz

To unpack the file you must run

tar -xvzf cmake-3.17.5.tar.gz

Then, to install cmake you have to run:

cd cmake-3.17.5/
./bootstrap
sudo make
sudo make install

This process can be quite long. To ensure that CMake is properly installed you can run:

cmake --version

This command should display the version of CMake that you just installed (3.17.5 in my case).

Configure WSL for CLion

You may need to enter the following command in the PowerShell to enable specific features:

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

Then, in the WSL command interface you have to configure and run the open ssh-server. To do so you can use a script.

wget https://raw.githubusercontent.com/JetBrains/clion-wsl/master/ubuntu_setup_env.sh && bash ubuntu_setup_env.sh

Then you must create a connection with:

ssh username@localhost -p2222

This command will ask you for a password (and username must be replace by your username).

Configure CLion

Now that everything is ready you must configure CLion in order to be able to use the CMake of WSL and then valgrind.

In File > Settings > Toolchains, click on Add (+) and select WSL. Your Debian environment should be detected automatically. However you will need to enter your credentials.

Set credentials on WSL toolchain in CLion

Set WSL as the default toolchain by using the arrows next to the + button (to move the WSL toolchain to the top).

Then everything, should be detected automatically. If it is not the case, you can change the cmake directory to \usr\local\bin\cmake.

In File > Settings > CMake, you must ensure that the toolchain which is used is WSL.

In File > Settings > Valgrind, you should set the variable executable to:

\\wsl$\Debian\usr\bin\valgrind

and the analysis options (that can be configured as you want) should have something like that:

--leak-check=full --leak-resolution=high --track-origins=yes --vgdb=no

Now, you can run your c / c++ files with valgrind memcheck by using:

Run an app with Valgrind Memcheck

You can then explore the results and solve the different issues.

Exploring the results of Valgrind Memcheck

Sources

--

--