Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Remotely Debug C++ Programs on RPI Using VS Code


Step 1 : Download and install the following items

  • Once you've opened VS Code
  • Click EXTENSIONS on the menu towards the left
  • Type in the search bar the following items and click INSTALL
    • Remote -SSH
    • C/C++
    • C/C++ Extension packs

image image image


Step 2 : Change The DISPLAY Envionment Variable

## 1) Open Powershell
  • Press Windows key
  • Type in search bar Powershell
  • Type in the following in your Powershell instance and the Notepad application should launch
    notepad $PROFILE
  • Enter the following in your Notepad instance
  • Save the written contents
    # You can pick whatever numerical value for local host
    # Example --> $env:DISPLAY = "localhost:10.0
    $env:DISPLAY = "localhost:10.0"
  • Restart Powershell
    • You MUST close and re-open PowerShell for the changes to take effect
    • To double check if the DISPLAY variable was saved, copy/paste the following
    $env:DISPLAY
  • Changing the PowerShell Execution Policy - Open Powershell - Enter the following bash Set-ExecutionPolicy RemoteSigned - You'll be prompted the following, just enter [Y] to confirm the action hit ENTER Execution Policy Change The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose you to the security risks described in the about_Execution_Policies help topic at https:/go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the execution policy? [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"):

1) Add DISPLAY Variable To Enviornment Variables in WINDOWS

  • Press Windows key
  • Type in the search bar ENVIRONMENT VARIABLES then press ENTER
    • Click on Edit the system environment variables
    • Once the Enironment Variables window pops up, click on new
      • For Variable name enter DISPLAY
      • For Variable value enter the local host value you set earlier in your NotePad instance
        • In this example, set localhost to localhost:10.0

image image image


Step 3 : Generate SSH Key On Your Host Machine/Workstation

1) Create .ssh 'folder'

  • Open Powershell press Windows key
  • Type in the following
mkdir .ssh

2) Generate SSH Key

  • Enter the following
ssh-keygen -t ed25519
  • When prompted :

    • Enter file in which to save the key (/home/local/.ssh/id_rsa)
      • Choose .ssh
      • Just press ENTER
    • Enter passphrase (empty for no passphrase):
      • Just press ENTER
    • Enter same passphrase again:
      • Just press ENTER again
  • You'll then be prompted with your key fingerprint and your key's randomart image as shown in the example screenshot below image

  • Find the directory where your newly generated SSH keys are stored

    • In my case, it's in the following directory
    • Copy the newly generated SSH key since we're going to paste it into a different file in the Raspberry PI/Linux device
 C:\Users\S123456789\.ssh
  • Use the ls command to list the contents of the .ssh directory
  • We're going to need the public key that has the .pub ending as shown in the screenshot below
  • You can use either VI or VIM to view the contents of your public key image

3) Save SSH Key on Raspberry PI or LINUX device

  • Type in the following commands in your Raspberry PI terminal
cd ~
sudo nano ~/.ssh/authorized_keys
  • If the .ssh folder DOESN'T exist, use the following commands
cd ~
mkdir authorized_keys
sudo nano ~/.ssh/authorized_keys
  • Then :
    • Paste your .pub SSH key into the authorized_keys file
    • Press Control + X --> Y --> ENTER to save and exit out of the file

4) Obtain your Raspberry PI IP address

  • Open a terminal on the Raspberry PI
  • Type in the following and take note of the value that's displayed under the wlan0 section
    ifconfig
    image

5) Enable X11 Port Fowarding On The Host Machine

  • On your host machine

    • Open a Powershell instance

    • Use the :

      • cd command to return back to the .ssh folder

      • -ls -a command to list everything stored in the .ssh folder

        • You should see a config file
          • If not, use the following command
          vim config
        • Use either vim or nano to edit the config file and enter the following WITHOUT THE BRACKETS
            Host [YOUR RASPBERRY PI IP ADDRESS]
                HostName [YOUR RASPBERRY PI IP ADDRESS]
                User [YOUR RASPBERRY PI USER NAME]
                ForwardAgent yes
                ForwardX11 yes
                ForwardX11Trusted yes
        
            # Example
            Host 192.123.56.111
                HostName 192.123.56.111
                User pi
                ForwardAgent yes
                ForwardX11 yes
                ForwardX11Trusted yes

6) Configuring VcXsrv Windows X Server

  • Upon running the setup file

    • Select Multiple Windows
  • Set the Display number to whatever value you set your localhost:10.0 to

    • Which in this case is 10
  • Select Start no client

  • Select Disable access control

    image image image


Step 4 : Configuring VS Code for remote development

  • Open VS Code - Right click on Remote-SSH

  • Click on Extension Settings

  • Ensure the following boxes are checked:

    • Remote.SSH: Enable Agent Forwarding
    • Remote.SSH: Enable Dynamic Forwarding
    • Remote.SSH: Enable X11 Forwarding image
  • Establish connection to host

    • Press Control + Shift + P
    • Type in the following : connect to host and choose either option
    • Click on Add New SSH Host
    • To verify that you're connected to the Raspberry PI you can :
      • Check the lower left corner and you should see the Raspberry PI IP address image image
      • Both a bash terminal and the IP address should show up as indicated in the screenshot below image
  • To open a project that's saved on the Raspberry PI

    • Click the first icon on the left side menu
    • Click on Open Folder
      • Then you can navagate to the Desktop where the repository should be stored image

Step 5 : Running the VS Code Debugger

1) Setting up the debugger

  • In VS Code find the run option
    • Then click on :
      • Add Configruation
      • C++(GDB/LLDB)
      • Add configuration
      • C/C++: (gdb) Launch
    • References for the mentioned steps can be found in the screenshots below image image image image
  • Afterwards you should see a .vscode folder in the EXPLORER menu
  • A launch.json file should be generated at this point
    • The only changes that need to be made is the "program" argument since the debugger needs to point towards the executiable file
      • Original version
      "program": "enter program name, for example ${workspaceFolder}/a.out",
      • Modified version
      "program": "${workspaceFolder}/main",
      image image

2) Running the Debugger

  • Set a break point anywhere in the main.cpp file as indicated by the RED ARROW image

  • There's two ways to run the Debugger

  • In the Run options you can manually select Start Debugger - Or you can just press F5

  • Once the Debugger runs

    • The yellow line indicates where you're at in the program
    • The menu circled in red does the following
      • The play button
        • When pressed, the program will run until it hits the next breakpoint
      • The step-over button
        • When pressed, if the next line the debugger is about to 'step into' is a function, you can esentially skip over it
      • The step into button
        • As the name implies, you can step into any function
        • Or manually step through every line of code to have a better understanding of what's going on with the program
      • The step out button
        • When pressed while in a C++ function
        • You can 'exit' out of the function and use a different debugging command
      • The restart and stop button
        • I'm pretty sure these buttons can explain themselves

image

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages