Editor's note: I am pleased to bring you a new young blogger on Planet Analog, Tyler Deuty. He will be occasionally blogging and bringing us a really good perspective from the younger generation. I bring his Open-Source/Linux-related blog to Planet Analog because it is so intertwined with Analog applications and the IoT. We have seen many IoT applications using Linux that are Analog-related such as Voice, Wireless or Infrared Control? and The Electronics Cooperative Concept, Part 1 and many more as well, so I think this is a good thing for Analog designers to at least consider in this new Millenium of electronic design. It’s another tool in our design tool-belt. Please share with our audience, in the comment section below, more ideas of Analog/IoT applications within this Linux app realm.
In November 2016, Microsoft joined the Linux Foundation. With the Anniversary Update for Windows 10 they also provided the result of their partnership with Canonical in the form of a Bash shell that runs on Ubuntu on Windows 10; a.k.a. the WSL or Windows Subsystem for Linux. This has opened the door for the long-sought possibility of using the Windows and Visual Studio pairing that developers are comfortable with to develop using the g++ compiler and GNU Project DeBugger (GDB) debugging. Another key advantage is that all of this can be done without the need for setting up a resource-heavy virtual machine.
I found this to be the main reason to switch to developing using the WSL due to my mobile development platform having a high-DPI screen that can be a pain to get a Linux Virtual Machine (VM) to make good use of as well as not having a lot of disk space for a virtual hard drive. I probably don’t need to tell you that being able to work on the Windows operating system but compile and debug using g++ and GDB is a groundbreaking and powerful capability. Compiling with g++ of course means that the application will be able to run on devices using the *nix operating systems, including many IoT devices.
This is one way to develop applications for the IoT in the Windows environment. Utilizing the WSL will allow you to push your code out to Raspberry Pi devices and other small form-factor devices that run *nix operating systems, without having to learn or re-learn how to setup and work in a Linux environment and IDE.
Below is the entire step-by-step process for getting started developing Linux applications with one computer running Windows 10. I have assumed that you already have Visual Studio 2015 installed as well as the Anniversary Update for Windows 10 or higher, which comes through Windows Update.
Installing the Windows Subsystem for Linux (WSL)
1. The first step is to make sure that you prepare your machine for development. To do this, go to Start -> Settings icon (gear) -> Update and Security -> For developers, then select the radio button for “Developer mode.”
2. Now we can install the subsystem. Right-click the start button and choose Programs and Features from the top of the list. On the left-hand side of the window, click “Turn Windows Features on or off” and then scroll down to Windows Subsystem for Linux (Beta). You’ll notice that it is in beta; it may well be that a lot of this tutorial will no longer be needed with future updates to this system. Windows will need to restart before you can continue with the installation.
3. After logging back in from a restart, go ahead and open a command line (Right-click start and select command prompt) and type in ‘bash’ without the single quotes. Type in ‘y’ to accept the install and then watch as Ubuntu is installed to your Windows 10 computer. Also, follow the prompts for creating a UNIX user.
4. You should now be able to hit start and type in ‘bash’ and pin Bash anywhere that is easily accessible for you in Windows. See this article for more info on how you can better integrate the WSL into your normal workflow on Windows.
Note: If you use Bitdefender firewall, you should disable it fully before you can update your WSL environment. You can safely re-enable and still SSH in with Visual Studio. This may also apply to any other anti-virus software.
Configuring SSH in the WSL
Now that you have a Linux environment living on your computer, it’s time to take advantage of it. For Visual Studio to compile and debug your code in WSL, it must be able to SSH into it. If you’ve never edited files in a Linux command line interface before, here is an exemplary tutorial on how to use VIM.
1. The first step is quite counter-intuitive: uninstall the SSH server program. To do this, open your bash shell and type:
2. In some cases, it’s necessary to ensure that your WSL is fully updated.
3. Now you’ll need it back, this time with a fresh install. Type the following to install it from bash:
4. The configuration for SSH needs to be adjusted to allow connections. Make the following changes in sshd_config (path to file in below command). If the line already exists, just comment it out and change its value. If it doesn’t, add it anywhere in the file. Add your new UNIX username in the place of ‘user.’ You can use any port for SSH that isn’t already used by something else.
5. Restarting the ssh service will apply these new changes to the configuration.
Your bash shell is now ready to accept incoming SSH connections from the local machine. You can download Putty here and try a connection to 127.0.0.1 using your custom port and see if it works. If it works here, there’s a good chance Visual Studio is now able to connect.
Creating a Linux Development Project in Visual Studio
1. First off you will need to have the “Visual C++ for Linux Development” installed to your Visual Studio 2015 IDE. To do this, go to Tools -> Extensions and Updates. Then click “Online” from the Navigation pane and search “linux” in the search box in the top right. Download and install the package by Microsoft. You may need to re-launch Visual Studio afterwards.
2. Now that the right packages are in place the cross-platform projects are available to create. To make a new Linux development project go to File -> New Project and choose Visual C++ -> Cross Platform -> Linux, and then the type of project you want. For this article, I chose the console application. As you can see there are other options such as Blink (Raspberry) or an empty project.
3. If you are used to developing in Visual Studio the new project should be very comfortable to you. The solution explorer shows an auto-created (and debug-able) main.cpp. In addition, there is an excellent “Getting Started” page, complete with animations covering the new features that came with the extension. The biggest differences can be seen in the toolbars; most notably the debugger. The next section will show how to put this to good use.
Connecting to WSL for remote compilation and debugging
1. Your bash shell needs to be ready to compile and debug C++ code. Install g++ and gdb to accomplish this.
2. In Visual Studio, open the project you made in the previous section.
3. Click “Remote GDB Debugger” press F5 to start a remote debugging session. You will be prompted to setup a connection.
If you want to compile on another Linux machine in addition to your WSL, you can go to Tools -> Options -> Cross Platform -> Connection Manager and add more connections there.
4. If you entered in the correct information for your WSL you can watch the output for signs of it copying over your files, compiling, and then debugging your C++ code in a Linux environment, all on one machine without the need for a bulky and resource-heavy virtual machine or an unfamiliar IDE.
5. If you want to see the output of your new program, simply go to Quick Launch (ctrl-q) and type in linux. Choose Debug -> Linux Console and you will have a Linux console within Visual Studio that you can use to interact with and view output from your Linux console applications.
6. IMPORTANT : If you close your bash shell, you will have to run the following to make sure SSH is ready to accept requests.
sudo service ssh start
This is approaching being as lengthy a process as installing a virtual machine with Linux on it, but in my opinion the end result is worth the initial setup. There are many students and developers out there who might not have access to a Linux server to compile with or the resources and time required to get a Linux virtual machine and IDE set up and operating at a level that can match their effectiveness with a Windows PC and the Visual Studio IDE. I expect to see more improvements in the future to the WSL and in Visual Studio 2017 that streamline this process and really make this setup a sound replacement for a hosted VM.