Using Visual Studio and WSL to Develop Linux Applications for the IoT

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 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

7. Done!

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.

5 comments on “Using Visual Studio and WSL to Develop Linux Applications for the IoT

  1. Victor Lorenzo
    February 4, 2017

    I work extensively on a Windows machine, not because I like it but because I'm forced to. I can hardly see why would anyone develop Linux applications on Windows using Visual Studio. I'd like to hear your opinion about it.

  2. tydeuty
    February 5, 2017

    Hi Victor,


    The short answer to your question is if anyone is planning on extensively and/or exclusively developing Linux applications, Visual Studio (at least at the current time) is not the way to go. However, if someone is a developer and is very used to the Windows OS and Visual Studio IDE who has taken an interest in writing Linux applications, they can do so using the features in the article.

    I have been a Linux system administrator, worked tech support for Apple, and I'm currently a Windows system administrator, so I've seen the pros and cons of each. I personally would use Linux for servers and Windows for personal and development machines. Windows has recently brought over many features that I had missed from using Linux distributions such as workspaces and the PowerShell. Linux-influenced features like these combined with the benefits of having a huge chunk of the market share make Windows a powerhouse in my opinion and the opinions of others. So if people are like me and enjoying (or in your case forced to use) Windows, but want to develop for Linux, they will be glad to have the ability to.

    I'm also most familiar with Visual Studio and have existing projects in it. I am a big fan of the customizable interface VS provides, the debugging experience is very helpful. I like being able to hover over a variable and getting its current value or having values updated highlighted in red as well as other features. It has support for third-party source control like Git and a great integration for GitHub. There's something to be said for maintaining consistency in your development environment while tacking on extra uses for it like developing for a different platform. My opinion is that the advantage here is not having to install, configure, and learn a new environment just to develop some applications for an IoT device that might be completely designed and developed in Windows.

    I also am personally against using virtual machines on my development machines. With SSDs gaining popularity, the disk space we were enjoying is not always there on our laptops for a second OS and IDE. Bringing the OS one step further away from the hardware is also a detriment. Most VM's have their resources limited to somewhere around half of what the machine they are residing on can do as well, so they underperform. My opinion was that logging in to a Windows machine with an industry-standard IDE and then booting up a VM just to write for a different platform was ridiculous. However, a dual-boot situation could remedy this issue.

    Here are some situations where I see the method in the article coming in handy:

    1. In your case, you are forced to use Windows. At least now you can write Linux applications from your cage!

    2. Many students studying programming are used to the Windows environment. I'm assuming that it is due to the market share of Windows machines that most of the programming courses are taught for Windows and Visual Studio and the students are asked to install a VM if they don't have a Windows machine. In later courses they are taught to write using the POSIX API for Linux applications and are asked to compile on the school's Linux server or in their own VM. I already have many fellow students using this method to avoid the time and energy it takes to install, configure, and learn a new environment mid-semester. You may be horrified to know that there are many students writing their C++ code using the Windows compiler and then just copying the source files to Linux and hoping it will work there as well!

    3.  If someone is a power user with Windows who can pump out work quickly leveraging that OS, they still can and now write Linux applications. If this person was not used to Linux they would have a steep learning curve that is now unnecessary until you get into extensively writing for Linux.

    All of that being said, if someone is going to be focusing on being a Linux application developer then of course they should dive in to the Linux operating system for their development machine. I expect to see a blurred line between Linux and Windows developing in the future now that Microsoft has partnered with Canonical and joined the Linux Foundation. Thanks for the comment!

  3. happyman
    February 6, 2017

    Great use of Visual Studio. It confirms the Microsoft strategy to move towards open source !

  4. tydeuty
    February 6, 2017

    Thank you, glad you liked it.

  5. tydeuty
    February 6, 2017

    Thanks for the feedback. It does look like MS is moving that direction, I believe they are working on incorporating SSH into the PowerShell through OpenSSH, as well as the projects listed here:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.