Installing & Using Visual Studio Code for C++ ... - Part 2 - using WSL
See my previous article here: Part 1 and the next here: Part 3
See a slightly updated version of the series in GitHub ( https://github.com/Adrian-LL/vscode-cpp-win-wsl)
Some time ago I began to explore Visual Studio Code with the intent to re-learn C++. I was looking for some simple IDE and I was not very pleased with CodeBlocks or NetBeans. VSC seemed to satisfy my requirements, but implied the installation of a C++ compiler & other tools needed to compile and run the programs.
I tried MinGW / MSYS / Cygwin, and I thought that, since Microsoft was offering Linux natively in the latest editions of Windows 10, to use WLS (Windows Subsystem for Linux). (Another alternative was to use the native Microsoft tools - but these seems to be too big and/or complicated - although the debugging is the best...)
Please note that we are still discussing about (re)learning programming, C++ in this case.
Also note that, when using wsl and Linux tools (such as gcc, g++) the default executables are Linux-native - so they will run on Linux, not on Windows. There are cross-compilers available.
The default executables compiled with gcc or g++ under Linux are Linux native ELF - not Windows exe files
Because my objective was mainly writing many independent programs that work using console input/output - not big projects, nor graphical ones, wsl seemed a logical approach.
So, please follow the next steps:
Prerequisites:
- You should be connected to the Internet, preferably with a good speed :-)
- Minimal knowledge about Linux would be nice.
Step 1 - install WSL (just query the net…). You should check that your Windows version is compatible with it.
You can see it by checking "Turn Windows features on or off" in the Control Panel.
And check the "Windows Subsystem for Linux"
Or you can use PowerShell as Administrator (important) with the following command
PS> Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
A restart is needed.
- Go to Store
- Search for "Linux" and choose "Run Linux on Windows".
- Install your preferred version. (I used Ubuntu for demo purposes)
Step 2. After installation, launch it:
- From the store
- From the start menu - Ubuntu or bash (please be careful if you already have some other tools installed such as MinGW or Cygwin or MSYS or something else - Mobaxterm or mintty comes to mind).
- There is even the wsl.exe command.
- You will get a screen similar to the next - now the "real" installation takes place
- It will take a few minutes (maybe more).
- You need to enter a user name - although you can use the same name as your Windows user, this will be a different user (the name and password are linux-specific)
Once Ubuntu is installed, you have to run the latest upgrades. If you use other Linux distros, the commands are specific to those.
user@station:~$ sudo apt install update && sudo apt upgrade
and actually install the C++ compiler and tools (we will use gcc)
user@station:~$ sudo apt install g++ make gdb
Even safer instead of the above, is to install the build-essential (note the missing 's') package. Also gdb is not includes so you have to install it separately.
user@station:~$ sudo apt-get install build-essential gdb
And now to the work - using Visual Studio Code:
If you din not already installed you can do it now.
NOTE - You may get an error about git, let's ignore it for the moment.
- Change the default shell to WSL shell:
- Via F1 command and Terminal: select default shell: (select the wsl bash option)
- Via vsc menu: File -> Preferences -> Settings -> search for terminal.integrated.shell.windows
- The C++ extension by Microsoft is installed in VSC. (See in Marketplace at https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools. The github page at https://github.com/Microsoft/vscode-cpptools)
//please ignore the errors...
#include<iostream>
using namespace std;
int main(){
cout << "Hello from VSC" << endl;
return 0;
}
If the default shell int VSC terminal is bash, the following tasks.json should work.
sample tasks.json for VSC + C++ + wsl
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "compile C++",
"type": "shell",
"command": "g++ -std=c++11 -g ${fileBasename} -o ${fileBasenameNoExtension}",
"presentation": {
"reveal": "always",
"panel": "shared"
},
"group": {
"kind": "build",
"isDefault": true
},
// Use the standard less compilation problem matcher.
"problemMatcher": {
"owner": "cpp",
"fileLocation": [
"relative",
"${workspaceRoot}"
],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}
]
}
For debugging purposes, a launch.json file is needed.
Remarks
- VSC is an editor, do not forget.
- The debugging in C, C++ in not natively implemented, so it depends on the Extension (C/C++ extension in this case).
- lldb (from llvm + clang) does not work under wsl as it seems to be based on some linux kernel semaphores and whatnot. (and WSL does not have a Linux kernel... see next part of this article series).
It follows in my experience that debugging does not really work (especially in this WSL setup). I managed to get gdb working with the following configuration, but w/o input from console.
sample launch.json (with comments)
{
// Use IntelliSense to learn about possible attributes.// Hover to view descriptions of existing attributes.// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387"version": "0.2.0","configurations": [
{
"name": "(gdb) Bash on Windows Launch",
"type": "cppdbg",
"request": "launch",
// I had to put the full path manually;
// it should had worked with variables (see tasks.json), but it didn't
"program": "/mnt/c/Users/Adrian/Documents/hello",
"args": [
"-fThreading"
],
"stopAtEntry": true,
// see comment about path above.
// I tried some workaround
//"cwd": "$(wslpath '{workspaceFolder}')" - this did not work...// "." works though
"cwd": "/mnt/c/Users/Adrian/Documents",
"environment": [],
// also tried the following with true or false, but no avail...
"externalConsole": false,
"pipeTransport": {
"debuggerPath": "/usr/bin/gdb",
"pipeProgram": "${env:windir}\\system32\\bash.exe",
"pipeArgs": [
"-c"
],
"pipeCwd": ""
},
"MIMode": "gdb",
// tried to give input from a file instead of console// did not worked, but I left it here for documentation purposes"miDebuggerArgs": "-ex 'start < arguments.in'","setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true,
}
],
// this seems to be IMPORTANT
"sourceFileMap": {
"/mnt/c": "c:\\"
}
}
]
}
As an alternative, install Easy C++ Projects from the marketplace. It already provides prebuild files for wsl. But beware, in this case you should keep the Windows terminals (cmd.exe or powershell).
Bibliography (not sorted :-)
- https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools
- https://github.com/Microsoft/vscode-cpptools/blob/master/Documentation/Getting%20started%20with%20IntelliSense%20configuration.md
- https://daverupert.com/2018/04/developing-on-windows-with-wsl-and-visual-studio-code/
- https://jessicadeen.com/badass-terminal-wsl-macos-and-ubuntu-dotfiles-update/
- https://blog.ropnop.com/configuring-a-pretty-and-usable-terminal-emulator-for-wsl/
- https://brianketelsen.com/going-overboard-with-wsl-metadata/
- https://stackoverflow.com/questions/44450218/how-do-i-use-bash-on-ubuntu-on-windows-wsl-for-my-vs-code-terminal
- https://medium.com/@johnwoodruff91/epic-dev-environment-with-wsl-dc81e234ae61
- https://www.hanselman.com/blog/WebDevelopmentAndAdvancedTechniquesWithLinuxOnWindowsWSL.aspx
The year of Linux on the Windows Desktop
Setting up a Shiny Development Environment within Linux on Windows 10
TO BE CONTINUED...