Visual Studio/Linux development
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
- use another Linux machine
- use Windows Subsystem for Linux
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.
Visual Studio 2019 can natively detect the presence of WSL. This means that while you still have to install WSL, you may not have to follow all of the other steps. |
Pre-requisites
editYou'll need
- Windows 10 (64-bit only)
- Visual Studio 2017 or later
Procedure
edit- Install the Linux development workload from Visual Studio Installer if you already haven't.
- 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.
- 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
- Open
/etc/ssh/sshd_config
using a code editor; for example,sudo nano /etc/ssh/sshd_config
. Look forPasswordAuthentication
and set it to yes. - Start the SSH service:
$ sudo ssh-keygen -A $ sudo service ssh start
- Open Visual Studio and create/import a Linux project:
- 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)
- 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).
If you are programming in C, make sure to set the file extension as .c for IntelliSense to work. Even then, you may get errors saying that common header files like stdio.h cannot be found; this is a bug and the program should still compile. |
Usage
editYou 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.