Using Visual Studio to set up a C/C++ remote development environment
Tags: cpp, ubuntu, ert3plus, raspberrypi
Published: 2022-08-03 05:35:53

## Introduction

In the previous articles, we have demonstrated a number of programs, mostly written in Python, that run on an Edge device. However, there are times when we need to write programs in C/C++. It is easy to set up a C/C++ remote development environment on an Edge device such as e-RT3 Plus or Raspberry Pi. In this article, we demonstrate how to set up the C/C++ environment on an Ubuntu installed Edge device using Visual Studio.

## Environment

**Supported devices (OS)**

- e-RT3 Plus F3RP70-2L (Ubuntu 18.04 32-bit)
  Edge controller of Yokogawa.
- Raspberry Pi 4 Model B (Ubuntu Server 20.04 32-bit）

The `armhf` architecture package runs on these devices.

**Software requirements**

Visual Studio Professional 2022 version 17.2.6

## Getting started

The first step towards setting up a Visual Studio environment on the device is to install the `gdbserver` package. To install the `gdbserver` package on the device, run the following commands.

```bash
sudo apt update
sudo apt install g++ make gdb gdbserver zip
```

> **Note**: By default, this package is installed on e-RT3 Plus.

## Installing Visual Studio

Follow these steps to install Visual Studio:

1. Install the tool set required to set up the Linux environment.

    Open Visual Studio Installer and install **Linux and embedded development with C++**.

    ![01_Install_Linux_dev](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kv3a156cz5hjpp9fvqba.png)

    For more information about Linux development with C++ in Visual Studio 2022, refer to the [official documentation](https://docs.microsoft.com/en-us/cpp/linux/?view=msvc-170).

2. Configure the settings for connecting Visual Studio and the device by performing these steps:

    1. Start Visual Studio.
    2. Open **Tools** **>** **Options** **>** **Cross Platform** **>** **Connection Manager**.
    3. Click **Add**.

        ![02_VisualStudio_config](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ol8bjatxpb4l4iiwnqsl.png)

    4. Enter the following details and click **Connect**.

        | Items | Value |
        |---|---|
        | Host name  | IP address of device |
        | Port | 22 |
        | User name | User name for device |
        | Authentication type | Password |
        | Password | User password |

        ![03_Connect_remoteSys](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x16oq0gipux4qpxzdf6n.png)

3. To download the header to be used in IntelliSense, click **Connection Manager** **>** **Remote Header IntelliSense Manager**, select the device that you added, and then click **Download**.

    ![04_DownloadHeaders](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ws575qm6o1egcni9n5jq.png)

## Creating and debugging a project

Creating and debugging a project involves the following steps:

1. [Creating a project](#creating-a-project)
2. [Configuring project settings](#configuring-project-settings)
3. [Building and debugging a project](#building-and-debugging-a-project)

### Creating a project

Follow these steps to create a project:

1. Start Visual Studio and create a new project.
2. Specify the project type as **C++** and **Linux**, select **Console Application**, and create a project by specifying the project name.

    ![05_CreateNewPjt](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tekwgo4p6vpfgcim0asi.png)

### Configuring project settings

Follow these steps to configure project settings:

1. From the menu bar, select **Project** **>** **{project-name} Properties**.

    ![06_Select_PjTProperties](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ozf8ch9sf1ea6yc8rav7.png)

2. From the Configuration drop-down list, select **Debug**.
3. From the Platform drop-down list,  select **ARM**.

    ![07_ConfigureProperties](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nkvrdxbzc826o7u921au.PNG)

4. On the left pane, select **General**.
5. On the right pane, from the **Remote Build Machine** drop-down list, select the added device.
6. Click **Apply**.
7. On the left pane, select **Debugging**.

    ![08_ConfigureDebugProperties](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vjvx3zl7o1ry5utg7kgs.PNG)
8. From the **Remote Debug Machine** drop-down list, select the added device.
9. From the **Debugging Mode** drop-down list, select `gdbserver`, and then click **Apply**.

### Building and debugging a project

Follow these steps to build and debug a project:

1. After selecting **Debug** for Configuration and **ARM** for Platform, place a break point anywhere in the source code to build and run the program.

    ![09_CreateSampleCode](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wzpuuhkx2gv8wkv155tz.PNG)

    If the program stops at the break point, it indicates that the project is created successfully.

    ![10_SampleCodeBreakpoint](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sh4zmc3cfvnbs7wqi7cw.PNG)

    The output is displayed in the Linux console window.

    ![11_SampleCodeOutput](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bulgj100nlq5xwmpke3y.png)

## Appendix

### Adding source files

Follow these steps to add source files:

1. Right-click the project icon, and select **Add** **>** **New Item**.

    The *Add New Item* dialog box appears.

    ![12_Addnew](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1taywifhcto8l5izis78.png)

2. Specify the file name, select the file type and then click **Add**.

    ![13_ItemName](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pjyetklvkupposjhusyv.png)

### Library links

Follow these steps to add library links:

1. Open the Project properties.
2. On the left pane, select **Configuration Properties** **>** **Linker** **>** **Input** **>** **Library dependencies**, and then click **Edit**.

    ![14_AddLink](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y8q1m3x2kybow54z25le.png)

3. Enter the name of the library to be added. 

    The following image shows the scenario when `libm` is added.

    ![15_LibraryName](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ammmuiyg7q4a9df9ocjb.png)

### Adding Include path

If you are using a header file that is available in a location other than the default location, you must configure the Include path settings.

1. From the Project Properties, navigate to **C/C++** **>** **General** **>** **Additional Include directories**, and then click **Edit**.

    ![16_Includepath](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/afesod3iosaiof2nr3oh.png)

2. Specify the directory path on device that includes the header file and click **OK**.

    ![17_Addpath](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y72ohd4otkt1nshyjcr0.png)

## References

1. [AI platform for industries | Yokogawa Electric Corporation](https://www.yokogawa.com/solutions/products-platforms/control-system/ert3-embedded-controller/#Overview)
2. [Linux development with C++](https://docs.microsoft.com/en-us/cpp/linux/?view=msvc-170)