Visual Studio/Linux development

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Recent versions of Visual Studio have included cross-platform support, notably for Android, iOS and now Linux. As Linux applications cannot (obviously) run natively on Windows, one can either

In this guide, we'll use the latter method as it's more 'native', though those wishing to use the former method can also use this guide.

Pre-requisites[edit]

You'll need

  1. Windows 10 (64-bit only)
  2. Visual Studio 2017 or later

Procedure[edit]

  1. Install the Linux development workload from Visual Studio Installer if you already haven't.
  2. Install the Windows Subsystem from Linux tools and download a Linux flavour from the Windows Store of your choosing Here's an example guide. Set up the Linux configuration.
  3. Run the following commands on the Linux bash:
$ sudo apt update
$ sudo apt install -y build-essential
$ sudo apt install -y gdbserver
$ sudo apt install -y openssh-server
  1. Open /etc/ssh/sshd_config using a code editor; for example, sudo nano /etc/ssh/sshd_config. Look for PasswordAuthentication and set it to yes.
  2. Start the SSH service:
$ sudo ssh-keygen -A
$ sudo service ssh start
  1. Open Visual Studio and create/import a Linux project:
    Linuxprojectvs1.PNG
  2. Open Cross-Platform Manager, and click Add. Enter the host name as localhost, port number as 22 and enter your UNIX username (not Windows!) and password. If you are not using Windows Subsystem for Linux, enter the host name of the Linux machine that you'll use instead of localhost. If all goes well, you'll see a message telling that IntelliSense is configuring libraries.

You need to have the Linux compiler installed on WSFL! Use appropriate apt-get commands as necessary to install them (eg: sudo apt-get g++). Otherwise, you'll get an error when trying to run (or after clicking Add in the previous step)

  1. Make sure that the target is set correctly, and then debug as normal. You'll see the output in the Linux Console Window on the bottom of Visual Studio (not in the bash shell or Command Prompt).

If you can't see any output, make sure that your program has at least one waiting state. This is because the Linux Console Window disappears, and its contents erased, when the program stops debugging (it has the same effect as debugging a normal Windows program through VS if you haven't configured it not to close immediately).

Usage[edit]

You can use it much like you'll debug a normal Windows program. The main exception is that features like CPU tracking are restricted, which is because the app is technically run elsewhere - on the Linux subsystem. One can easily set breakpoints, watch, locals and autos as normal. When (if?) a segmentation fault occurs, Visual Studio will halt the program and show the nearest location where the fault could have occurred (like what you'd get if you used gdb and enabled debugging symbols).

It is often possible to customise debugging settings. To do so would be the same as it's done on Windows - by going to Project Settings.

References[edit]

  1. [1]