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.|
- Windows 10 (64-bit only)
- Visual Studio 2017 or later
- 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
/etc/ssh/sshd_configusing a code editor; for example,
sudo nano /etc/ssh/sshd_config. Look for
PasswordAuthenticationand 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.
- 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 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 |
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.