使用VisualGDB在VS上编写Linux程序

As a C++ developer and Linux user, I often write code on Linux. Recently, I discovered VisualGDB (a VS plugin), which is a fantastic tool that allows me to write and run programs directly on Linux from Windows! The debugging experience is even better!

The reasons I like using Linux, apart from its useful shell and the need to write code that runs on Linux, seem to be pretty trivial (isn’t this a bit obvious?). Although coding directly on Linux is indeed enjoyable (with SublimeText or Clion), from the IDE standpoint, there’s no comparison—Windows’ Visual Studio is unmatched; it is simply the best IDE in the universe, bar none.

I had previously wondered if it was possible to develop Linux programs directly on Windows, but the results were always unsatisfactory, and the experience was not great. Cygwin was too sluggish, and using a virtual machine for a Linux environment (SublimeText + Shell) was also disappointing. I do have a Raspberry Pi on hand, which I can SSH into for coding and compiling (I can only edit source files locally and then transfer them for manual compilation). However, the SSH tool experience is significantly worse compared to using SublimeText + Shell directly on Linux, although Xshell on Windows has been quite good.

Without further ado, let’s look at the features of VisualGDB:

With VisualGDB, we can create four different types of projects:

The only two project types I need are Linux Project Wizard and MinGW/Cygwin Project Wizard.

When we select the Linux Project Wizard, we also need a device that can be connected via SSH (such as a virtual machine, VPS, Raspberry Pi, etc.), as the project we create will actually be executed in a Linux environment. However, we can debug/run in Visual Studio to control the project running on Linux.

To create a MinGW/Cygwin Project Wizard project, we need to install MinGW and Cygwin locally (on Windows), using the local MinGW and Cygwin environment to compile and execute. I’ll set this aside for now.

For the device that requires SSH connection, I am using a Raspberry Pi. I feel that the Raspberry Pi is truly an amazing coding tool; with it, there’s no need to run a virtual machine, and it can also be used in an offline environment (connecting to a VPS requires our computer to be networked). When using it, as long as both the computer and the Raspberry Pi are on the same LAN, it’s incredibly convenient (haha).

If you’re interested in setting up a Raspberry Pi, you can check out my previous blog post: Turning the Raspberry Pi into a Portable Linux Compilation Environment

Here are several images to illustrate how to create and run a Linux project in VS using VisualGDB:

Choose the specific type of project and compilation environment we need (C++11 is recommended):

Enter the details for the SSH-capable device:

After entering the device information, it will automatically SSH into the device and create our project folder (under the /tmp/VisualGDB directory); the generated files will be deleted when the Linux device restarts (which means the /tmp/VisualGDB project will be deleted upon reboot). Fortunately, our code remains local; only the compilation will sync the code to the Linux device, and even after deletion, it will still sync upon recompiling/executing, so there’s no need to worry.

Select the project storage paths for both local and remote hosts (the defaults are recommended):

Once the project is successfully created, it will automatically open the source file (if it’s empty, you will need to add the source file yourself). We can try compiling and running it:

While running, you can also open the SSH console, which essentially means that VS has an integrated SSH client; the level of integration is high, making it very enjoyable to use.

You can set breakpoints directly in the VS for the Linux project and check local variables and other information:

In the settings, you can also change the compiler (Clang is recommended for friendlier error messages):

Now, you can start coding!

Additionally, you can modify the project settings to use VS code suggestions:

Project - right-click - VisualGDB Project Properties

And also Project - right-click - Properties

In the Include Directories section, add a path: C:\Users\username\AppData\Local\VisualGDB\RemoteSourceCache\yourVPShost\0000\include, where username and yourVPShost should be modified according to your situation. For example, the path I need to include is:

1
C:\Users\visionsmile\AppData\Local\VisualGDB\RemoteSourceCache\192.168.137.2\0000\include

Additionally, here’s a tip regarding VPS/virtual machines/Raspberry Pi:

You can use Samba to share files with Windows (which makes it easy to transfer code; using FTP is still cumbersome, as you cannot directly edit files on FTP—it involves downloading a cached version to modify and then sync back, which is quite inconvenient).

You can install it as follows:

1
sudo apt-get install samba

After installation, you can edit the Samba configuration to create a shared folder:

1
sudo nano /etc/samba/smb.conf

Here’s the configuration:

1
2
3
4
5
6
[Linux]
path = /home/pi
writeable = yes
read only = no
guest ok = yes
comment = Created by Raspberry

The name in brackets Linux is the name of the shared folder, as it appears when we map a network drive in Windows. Path is the path you want to share; writeable and read only indicate read/write permissions for the shared folder. Setting guest ok to yes allows anonymous users to access it (no account password required, though you can configure it otherwise).

After modification, save and restart the Samba service:

1
sudo /etc/init.d/samba restart

Then, we can map a network drive in Windows to connect to the shared folder:

Note that if you connect and only have read permissions without write permissions, you need to SSH into the Linux device and change the permissions of the shared folder to 777 (as configured above):

1
2
3
cd /home/pi
# Change the permissions for all files and subfolders in the current directory to 777
sudo chmod 777 * -R

Then reboot the device.

Quickly Map a Network Drive in Windows

Through Samba, we can create a shared folder to store our code/files. However, manually mapping the network drive each time can be a hassle, and being lazy, I wrote a batch script to connect quickly (provided the device is networked or on the same LAN):

1
2
3
4
5
6
7
8
9
10
@echo off
net use Z: \\RASPBERRYPI\Linux 2>NUL
if %errorlevel%==0 (
start Z:\
) else (
echo If you don't have connected.
echo Please SSH connect to your device.
echo If you have connected, Please ignore the message.
pause
)

Modify according to your configuration and save it as *.bat. You can execute it when needed to map to the network drive (the shared folder we created).

The article is finished. If you have any questions, please comment and communicate.

Scan the QR code on WeChat and follow me.

Title:使用VisualGDB在VS上编写Linux程序
Author:LIPENGZHA
Publish Date:2016/08/08 00:53
World Count:5.1k Words
Link:https://en.imzlp.com/posts/9932/
License: CC BY-NC-SA 4.0
Reprinting of the full article is prohibited.
Your donation will encourage me to keep creating!