Visual Studio for existing remote Linux C++ projects using GDB

Introduction :

This post describes how to create a project on a remote Linux and also Windows subsystem for Linux , build it remotely and also debug it from Visual Studio step by step.

I previously used Netbeans to debug Linux C++ applications via remote SSH and GDB from Windows :

https://nativecoding.wordpress.com/2014/10/24/configuring-a-debian-virtual-machine-for-linux-c-development-via-windows-step-by-step/

However I find VisualStudio setup is easier and more convenient to use.

Linux side :

The only setup you need in your Linux is an SSH server. You can either use password based authentication or public key based authentication from Visual Studio. See section 4 of previous blog post for an example setup on Debian :

https://nativecoding.wordpress.com/2014/10/24/configuring-a-debian-virtual-machine-for-linux-c-development-via-windows-step-by-step/

It is written for Debian , however it is mostly same in other distributions. In case you want to use Windows Subsystem for Linux in your Windows10 or directly an Ubuntu ( jump to step C ) :

a) Install WSL , either Ubuntu or OpenSuse following this document : https://docs.microsoft.com/en-us/windows/wsl/install-win10

b) After installation , you will have to create an account with password authenticaton. Go to default Windows command prompt and type :

lxrun /setdefaultuser

Then specify a username and password.

c) You will want to install g++, gdb and SSH :

sudo apt-get update
sudo apt install -y build-essential
sudo apt install -y gdb
sudo apt install -y openssh-server
sudo service ssh –full-restart

 

CentOS : The main Linux distro I use is Centos 7. I had to update my existing openSSH daemon and restart it at least once to connect to it from Visual Studio :

yum install openssh openssh-server openssh-clients openssl-libsyum install openssh openssh-server openssh-clients openssl-libs systemctl restart sshd.service

Visual Studio side :

You can use either Visual Studio 2015 Community Edition and Visual C++ for Linux extension here : https://www.gallery.expression.microsoft.com/725025cf-7067-45c2-8d01-1e0fd359ae6e

However I would suggest using Visual Studio 2017 community edition for a few reasons :

a) You don`t even need an extension . Remote Linux setup comes with VS2017.

b) Whenever I debugged a remote Linux project with VS2015, the debugger will stop in certain unhandled Linux signals. Unfortunately I could not find a way of surpressing those. However you won`t have this issue with VS2017.

You can download VS2017 community edition from here : https://www.visualstudio.com/vs/cplusplus/

1 . You have to choose “Makefile project ( Linux ) ” in VisualStudio 2017 in order to use an existing project :

vs2017_linux_new_proj

2. You will have to add all files to your VS solution. You can either add the existing files from remote if Linux server has Samba . In case you can`t do that you can also copy the project to your Windows system and add all files to your solution :

vs2017_samba

3. Go to your project`s settings and choose “Remote build”. You will need to specify the remote build command. That command will be running in home directory of your SSH connection. An example :

vs2017_build

4. Go to your project`s settings and choose Debugging tab. Here you need to specify the binary output to debug for GDB. Paths you enter will be relative to the home directory of your SSH account :

vs2017_debug

Note that you can either use “gdb” which will be driving gdb via SSH or “gdbserver” . Just “gdb” is the method I use in this post. However if you want to run gdbserver ( it is independent from GDB ) in remote Linux whereas you are unable to install GDB,  you can also use “gdbserver” mode 6o connect to GDBserver running in your remote Linux.

5. An extra step at this point is setting your include path. That is extremely useful as you can feed VisualStudio`s s intelliSense and navigate native Linux headers. For simplicity , I copied my Linux` /usr/include directory to my local Windows and specified that path as one of include path in the project settings :

vs2017_intellisense

6. Finally , the last step is connecting to your Linux server. When you press debug button for the first time , you can connect using the connection screen :

vs2017_ssh

And finally this is how it looks :

vs2017_linux_debug

7. Linux console interaction : If you go to Debug -> Linux console , it will bring up Linux console window that you can interact with :

vs_linux_console

What about Visual Studio code :

Visual Studio Code is a great lightweight development environment that you can use for almost anything. I tried using from Windows targeting a remote Linux that runs gdbserver however couldn`t get it working. My current understanding is that it is not currently supporting remote GDB sessions from Windows to Linux server , however you can still connect to remote Linux/MacOSx servers from Linux/MacOSx clients as described here :

https://medium.com/@spe_/debugging-c-c-programs-remotely-using-visual-studio-code-and-gdbserver-559d3434fb78

Advertisements

7 thoughts on “Visual Studio for existing remote Linux C++ projects using GDB”

  1. I am unsure about “windows host” . The example shows running Visual Studio on Windows as host and connect to a remote Linux.

  2. I am not aware of any GDB limitation. You can choose the mechanism to drive GDB which is either connecting to GDB server or GDB-MI interface , I am guessing both protocols are quite standard since early versions of GDB.

    As for GCC I wouldnt expect any limitation as all it does is running an external command on the remote Linux

  3. Hi akhinn,

    Thank you bery much for all answers!
    If I may please ask one last question on this topic….

    I previously was familiar with commercial solutions to this remote debugging such as: VisualGDB and winGDB.
    It is that this is a free Microsoft solution for remote debugging of Linux application which kind of alternative to visualGDB and winGDB ?

    Thank you very much!
    Ran

  4. Yes , I would call it as a free version of visualGDB starting from VisualStudio 2015. ( I dont know about winGDB )
    Though one issue I had with Microsoft`s one is its SSH client might not be compliant with old openssh s on some systems. On the other hand visualGDB is probably has more features such as Linux driver development and also supports all Visual Studio versions

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s