Getting Started with Clang and Visual Studio Code on Windows with MSYS2 and MinGW-w64
Posted on June 11, 2021 by Paul
This is a short introduction in getting started with Clang on Windows 10 under MSYS2 and MinGW-w64. The Clang and LLVM binaries from https://llvm.org/ require that you to have Visual Studio 2019 installed on your machine, MSYS2 is a lighter alternative. In the last part of this article, I will show you how to use VS Code to build and debug a simple C++ program.
This article is split into three parts:
- Install MSYS2, Clang and GDB
- Compile and debug a simple C++ program from the MSYS2 terminal
- Compile and debug a simple C++ program from VS Code
Start by installing MSYS2 from https://www.msys2.org/, the installer will guide you through the required steps. Once the installation is finished, start an MSYS2 MSYS terminal from the Windows start menu:
Update the system with:
and accept the updates by writing Y.
Repeat the above command one more time to be sure that the system is fully updated.
Next, we are going to install Clang and GDB:
write Y when asked if you agree to install the required dependencies.
At this point, we are done with the installation. Close the MSYS2 MSYS terminal by writing:
Compile and debug a simple C++ program from the MSYS2 terminal:
From the Windows start menu find MSYS2 MinGW UCRT 64-bit and start a new terminal:
Alternatively, you can open the same terminal from the disk C → msys64 → ucrt64.exe.
Check the version of the installed Clang with:
This is what I see on my machine:
Create a folder named cpp_test and inside, a new file named test.cpp with this content:
You can build the program with:
This is what I see on my machine if I build and run the above program:
If you want to debug the code directly with GDB try this:
Add a break point to the start of main with:
Run the program to the previously added breakpoint with:
Advance three lines with:
You can visualize the content of the variable a with:
Or, if you want to see the content of the vector V:
If you want to run to the end of the program write:
and exit from the debugger with:
This is what I see on my machine if I run the above commands:
Compile and debug a simple C++ program from VS Code:
In the last part of the article, I will show you how to build and debug the above simple C++ program from VS Code. Download VS Code and install it on your machine. You can accept the default options.
Next, make sure you have a MSYS2 MinGW UCRT 64-bit terminal open, see the previous section.
We are going to add VS Code to the MSYS2 path. This is important because it will let VS Code know about the compiler and debugger that are available from MSYS2. Without this step, VS Code will not know from were to get your compiler and debugger.
Open the file .bashrc from the Home folder, here is an example of doing that using the nano text editor:
Scroll to the end of the file and write or paste the next line:
Save the file by pressing CTRL+O, exit from nano by pressing CTRL+X.
Next, write this in the terminal to enable the above modification:
Navigate to the folder where you saved the C++ program in the previous section and erase the existing executable:
Start VS Code in this folder with:
From the VS Code left panel, select the file test.cpp. If prompted to install the C/C++ extension accept it, otherwise press on the extensions button from VS Code and search for C++. You want to install C/C++ from Microsoft.
Once the extension is installed, press on the Run and Debug button. Press again on Run and Debug. Select C++(GDB/LLDB) when presented with a list of environments, next select g++.exe - Build and debug active file. This will create a new folder named .vscode in your project folder.
Select the Explorer button from VS Code to see the files that are in cpp_test. Click on .vscode, you should see two files in there launch.json and tasks.json.
Open tasks.json and find the command property:
Change g++.exe to clang.exe. It should look like this:
Next, find the args array
Add a few extra options:
This is the full content of my tasks.json:
Next, click on the first line of the main function to add a breakpoint (left of the line numbers). You should see a red dot:
Press the Run and Debug again. VS Code should compile your program, run and stop at the breakpoint. In the left panel you can see the local variables of the current function which are not initialized at this point. Step three times by pressing the curve arrow from the top middle of VS Code.
Now, you can see the content of the vector V, if you click on it.
This is what I see on my machine:
If you want to continue the program, you can press the left triangle from the above middle panel.
If you want to learn more about VS Code check the documentation or leave me a comment with a specific question.