Edit and Run C++ code on WSL in Windows 10 using CLang
Since the appearance of Bash on Ubuntu
on Windows 10, I wondered if it was possible to compile C++ code in Windows 10 using a standard Linux configuration - but without using a Linux partition or Docker.
It’s a bit tricky, but it’s possible.
To begin with, we setup a Windows 10 machine to compile and execute Linux C++ code compiled with CLang using Bash on Ubuntu
, also known as Windows Subsystem for Linux
(WSL) provided by Windows 10.
To setup WSL you can follow these instructions from Microsoft site:
https://msdn.microsoft.com/en-us/commandline/wsl
WSL installed? Can you open your newly installed Ubuntu terminal? Very well, let’s go on.
First of all we need to install CLang onto WSL. To know what “clang” packages are available in WSL, we can just type:
> clang
On my machine I get:
* clang-3.3
* clang-3.4
* clang-3.5
Let’s go ahead and install version 3.5
> sudo apt-get install clang-3.5
If everything went just fine, you should now be able to launch a very simple clang command, like:
> clang-3.5 --help
and get something like this:
Now we can edit our first program in C++. We will just use our preferred editor on Windows and then compile using the WSL Command Line tool.
Let’s try with a simple C++11 source code that computes the number of uppercase letters in a string:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
int main()
{
char example[] = "Hello WSL World!";
int nr_ucase = 0;
for_each( example, example+sizeof(example), [&nr_ucase](char c)
{
if (isupper(c))
nr_ucase++;
});
cout << nr_ucase << " uppercase letters in: " << example << endl;
return 0;
}
(you can find other C++11 examples in my cpp11learn project on GitHub.)
Open your favourite editor, I like Visual Studio Code so I’m using that. Copy and paste the code above, as shown below:
There is a big problem, now. WSL is not built to allow interoperability with Windows. This means that even if you save your code into the WSL User Home directory, which is located here:
C:\Users\[your username]\AppData\Local\lxss\home\[username]
you will not be able to see it in WSL (try it! Note that /lxss/ is a system, hidden directory.)
You can use the Powershell the get to view the directory contents
but any modification made by Windows inside that directory will not be available in WSL. (Here you will find an interesting article that explains everything about it.)
So, how can we solve the issue? The trick is to use a Windows folder to make the editing, and then mount this folder in WSL.
First of all, we create a new folder using Powershell, for instance:
mkdir C:\temp\cppcode\uppercase
and then save our code with Visual Studio Code as:
C:\temp\cppcode\uppercase\main.cpp
Any Windows directory is mounted by default in WSL:
C:\ -> /mnt/c
D:\ -> /mnt/d
...
so we will find our file in WSL:
ls -al /mnt/c/Temp/cppcode/uppercase/main.cpp
We are finally ready to use our CLang compiler. Of course, in a real world project we will setup a CMake script in order to compile and link our source file(s), but for this small example it will be sufficient to launch:
cd /mnt/c/Temp/cppcode/uppercase/
clang++-3.5 main.cpp -o uppercase -std=c++11
and finally run the executable with
./uppercase