Как установить valgrind clion
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:
Then, to enable the Virtual Machine feature you must run:
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:
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:
Then, you can download and install the Debian distribution:
With this app you get Debian for the Windows Subsystem for Linux (WSL). You will be able to use a complete Debian…
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:
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/.
To unpack the file you must run
Then, to install cmake you have to run:
This process can be quite long. To ensure that CMake is properly installed you can run:
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:
Then, in the WSL command interface you have to configure and run the open ssh-server. To do so you can use a script.
Then you must create a connection with:
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 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:
and the analysis options (that can be configured as you want) should have something like that:
Now, you can run your c / c++ files with valgrind memcheck by using:
You can then explore the results and solve the different issues.
Valgrind memcheck
Valgrind Memcheck is a tool for detecting memory-usage problems such as leaks, invalid memory access, incorrect freeing, and referencing undefined values.
Valgrind integration in CLion works on Linux, macOS, and Windows via WSL (see Valgrind on WSL).
Configure Valgrind
Install Valgrind on your system.
For WSL, install Valgrind on your WSL instance ( sudo apt-get install valgrind ) and provide CLion with the path to the executable (see next steps).
CLion will attempt to detect the Valgrind binary by searching in standard locations defined in the system PATH variable.
In case of a non-standard Valgrind location, set the path manually in the Valgrind executable field.
Specify analysis options or use the default ones.
Optionally, configure the list of the suppression files. For example, you may want to add a suppression file to turn off the Valgrind checks for some particular libraries.
Configure Valgrind on WSL
To use Valgrind on Windows via WSL, provide the ‘subsystem’ path to Valgrind, which is /usr/bin/valgrind by default, instead of the actual Windows location of the Valgrind binary. However, this path will not be valid until you select the CMake profile connected to the WSL toolchain. For this, do one of the following:
Set the WSL toolchain as default. This way, it will automatically connect to the default CMake profile:
Create a separate CMake profile, connect it to the WSL toolchain, and select this profile in the configurations switcher:
Run targets
To run a target (application or test) with Valgrind Memcheck, do one of the following:
Select the desired Run/Debug configuration and call Run | Run ‘your_target_name’ with Valgrind Memcheck from the main menu or click the dedicated toolbar icon:
If Valgrind was not configured previously, CLion will open the configuration dialog.
Valgrind might not work correctly when run on a project with Google sanitizers. CLion will warn you about that:
Explore results
When the launch is finished, open the Valgrind tab in the Run tool window.
The left-hand pane shows the list of all problems found by Valgrind Memcheck. Each entry includes the instruction pointer, problematic function name, and (if possible) the source file where the problem is located.
In the right-hand pane, you can find full stack frame description and the corresponding code preview (when possible).
Press F4 to jump back to the source code.
Export and import results
To export the results, click the Export icon in the left pane of the Valgrind tab and specify the destination XML file:
Note that the Export button is unavailable during execution.
To import the results, select Run | Import Valgrind XML Results from the main menu and choose the XML file to import from.
Tutorial Setup CLion for Windows
CLion Installation
Create an educational account using your university email:
Download, install and login into CLion.
Setup Windows Subsystem for Linux (WSL)
Open Windows Powershell in Admin mode by using Win+X shortcut and use the following command (In this guide we will install WSL version 1, this saves us a lot of troubleshooting. Feel free to try version WSL 2 if you like it)
Reboot your system
Choose your preferred distro (I suggest using Ubuntu because most of packages are updated and accessible, if you want a lightweight heavy distro use Debian) and download it from Microsoft Store.
Once installed, open it by searching it in Start Menu.
Create a UNIX user with a password (You need it later so choose one you can actually remember).
Update the distro with:
Package installation
Install packages with this command:
Configure CLion
Create a SSH profile with the following command (this assures that CLion connects to WSL):
Version 2021 and newer
It should detect automatically your WSL distro and version like the following screenshot:
Version 2019
Go to credentials, open the menu and configure a new connection:
host: localhost,
username: WSL username previously created,
port: 2222,
authentication type: WSL password previously created.
Test the connection with the related button.
If CLion is connected correctly it should automatically detect all the paths, otherwise you can set them manually (they are located under /usr/bin )
Setup Valgrind on CLion
Now you can run your program with Valgrind by clicking on the top right icon (I use Atom Material Icons plugin which I strongly recommend:
Redirect Input from file on CLion
Under Edit Configurations inthe top right corner you can change to redirect the input from file like the following images
Setup WSL Terminal on CLion
If you want to run CLI commands directly into the project directory you can use the built-in terminal in CLion.
or this if you have multiple distros installed:
Setup WSL Xserver [Optional]
If you want to run kcachegrind, massif-visualizer or any Linux app with a GUI follow the next steps.
/.bashrc ) file and append this line at the end of the file:
This will forward your Linux GUI to the Xserver (installed later).
Download and install VcXsrv Xserver (any other Xserver is fine, I just prefer this one): https://sourceforge.net/projects/vcxsrv/files/latest/download
Launch it and choose Multiple windows (just continue, all the other options are not relevant for our purpose) and then press Finish.
You should see in the bottom right on the taskbar the Xserver icon indicating it’s active.
NOTE
Remember to run XLaunch after every reboot to use it again
Troubleshooting GUI errors
cannot open shared object file: No such file or directory.
If you encounter the following error:
while running kcachegrind, fix it by executing the following commands:
Could not load the Qt platform plugin «xcb» in «».
If you encounter the following error:
Congratulations 🎉 you have installed WSL2! (naughty user, you have not followed my guide 😠!).
Replace the previous export with:
How to Run Valgrind in CLion, for C and C++ Programs
I needed this for myself, and Googled around but didn’t find anything satisfactory, but now after a little work, I’m all set with Valgrind.
Wouldn’t you love this on your toolbar?
Well, you can have it, friend. Read on.
1. Add this Bash script
I have it in my current project, because I’ve got multiple subprojects in one project, but you can put this script in your home directory or wherever.
What it does: The script takes the string C or Cpp as an argument. It then compiles your program and runs Valgrind on the executable.
Make it executable: chmod +x valgrind.sh
Update the script as necessary. This script handles both C and C++. It uses main.c for C and main.cc for C++.
2. Add it to CLion as an external tool
Go to Preferences > Tools > External Tools.
Click the + to add an external tool.
3. Add the button to your toolbar
By default, the tool you just made lives under Tools (on main menu) > External Tools. But navigating to that every time stinks.
Right-click your toolbar, click Customize Menus and Toolbars.
Click Main Toolbar.
Find the spot where you want to add the icon. Select an item and click Add After.
Now you have to find the tool to add there. Click External Tools.
Select it and click OK. it will complain that there’s no icon. That’s OK. The default is a cute green alien.
And it’s been added!
4. Push the button
Now when you click the button, it will compile in the working directory and run Valgrind on your executable.
Note the lost memory there is not part of my program’s memory, as some by-product of running it on a Mac. On Linux lost memory is all 0. I keep my eye on these only on Mac:
Play around with not freeing memory and you’ll see what I mean.
John Washam
Sr. Software Engineer at Zappos. Founder of TalkToTheManager and zKorean. I like fountain pens and nice paper.
Share this post
Important: Pick One Language for the Coding Interview
I wanted to clarify the programming language requirement for the interview. I was under the impression that I needed…
Dijkstra’s Algorithm in Python 3
Greed is good. And Dijkstra’s algorithm is greedy. Dijkstra’s algorithm not only calculates the shortest (lowest weight) path on…
CLion 2017.3 EAP: Valgrind memcheck integration
Today we are rolling out a new CLion 2017.3 EAP build (173.3415.24) with support for memory check from Valgrind and some polishing of the toolchains rework.
Valgrind: memcheck
Good news everyone! CLion now comes with Memcheck integration; this means you can detect runtime errors and memory leaks right from the IDE. We know you are eager to try it out on your project, but first let me put a few general thoughts down here:
System requirements & settings
Valgrind works on Linux and macOS and is available in CLion on both of these platforms. CLion detects it automatically by searching for it in the standard locations defined by the system PATH variable. Or you can point CLion to Valgrind’s executable using: Settings/Preferences | Build, Execution, Deployment | Valgrind:
Options with which to run Valgrind and a list of suppression files which can be used, can also be configured on this page. For example, you may want to add a non-default suppression file to turn off Valgrind checks on some particular libraries you are using in your project. The typical case here, for example, would normally be to suppress OpenSSL or Boost.
Default Valgrind options include:
Running targets under Valgrind
Select a Run/Debug configuration and run it under Valgrind memcheck via the Run | Run Valgrind Memcheck… or special icon in the Navigation bar located next to the Run and Debug options:
This icon is present on all three platforms, but it is disabled on Windows as Valgrind is not available there.
You’ll get a new tab called ‘Valgrind’ in the Run toolwindow. It provides the information about all the possible errors found by Valgrind, along with the source code location (use F4 on Linux and ⌘↓ on macOS to Jump to Source) and a stack trace. The results are grouped by the type of leak encountered:
In the context menu on a particular error you can:
Running tests under Valgrind
Google Test, Catch and Boost.Test targets can also be run with memcheck. You can either select necessary test target and press ‘Run Valgrind Memcheck’ icon in the Navigation bar, or use test icons in the left gutter when the file with tests is opened in the editor:
Check out the memcheck (Valgrind) integration into CLion and let us know what you think! Mind, UI is not final yet, so your feedback here is appreciated.
Toolchains, CMake profiles & more
Multiple toolchains support was brought in with CLion 2017.3 EAP which allowed:
This EAP build polishes these changes and allows you to get several CMake profiles with the same build type. For example, you can use it if you want to build different Debug targets in your project with different toolchains. In previous CLion versions, you have to switch the toolchain in the CMake profile that corresponds to the Debug build type. Now you can configure several such profiles:
That’s it! Full release notes can be found here.
Your CLion Team
The Drive to Develop