projects github about
Using Visual Studio Code to Run and Cross-Compile a C++ App for Raspberry Pi 3

Been using nano editor to create a c++ app on Raspberry Pi 3. And just last month decided to use Visual Studio Code instead. Not sure if really possible at first and haven’t tried it before when i’m trying out .Net Core 2, so i search the net for clues. Primarily running on Ubuntu 18.10 this time around.

By the end of this post… We will be able to Cross-Compile then Run our App remotely from a Raspberry Pi 3 and then display the output within the VSCode terminal.

Prerequisites

1
2
3
visual studio code for Linux
cross-toolchain - either Linaro / GNU ARM Embedded Toolchain / From ubuntu Repo
sshpass - also install this additional package
I'm using the GNU ARM Embedded Toolchain for arm32

Installation

Get the Visual Studio Code deb file from the main site.. https://code.visualstudio.com/download and install using the code below:

1
sudo apt install ./code_1.30.1-1545156774_amd64.deb

For the toolchain any of the three i mention above will do and the procedure is also the same when configuring the Visual Studio Code for our purpose. Same for arm64.

1
2
3
4
5
6
7
// GNU ARM Embedded Toolchain : AArch32 target with hard float (arm-linux-gnueabihf)
wget https://developer.arm.com/-/media/Files/downloads/gnu-a/8.2-2019.01/gcc-arm-8.2-2019.01-x86_64-arm-linux-gnueabihf.tar.xz?revision=c69ea519-a965-4359-84a4-cbd440ff4130&la=en

tar xvf gcc-arm-8.2-2019.01-x86_64-arm-linux-gnueabihf.tar.xz

// Or extract to your desired folder
tar xvf gcc-arm-8.2-2019.01-x86_64-arm-linux-gnueabihf.tar.xz -C desired-path

After Visual Studio Code installation, Run it and install these VSCode extensions…

1
2
3
c/c++
c++ intellisense
Easy C++ projects

VSCode Configuration

Now let’s create a sample C++ App for our Raspberry Pi using the Easy C++ projects extension that we installed earlier.

1). First is to have our desired project folder or if none create one.

1
mkdir sample-app

2). Then on VSCode open our project folder

3). Press Ctrl+Shift+P and type easy. Then select Create new C++ project

3). Then select [G++/GDB] Linux

4). We will then modify some auto-generated files… launch.json, task.json, Makefile

1
2
3
4
// Makefile
// edit this line with
CXX := g++  to  CXX   := arm-linux-gnueabihf-g++
EXECUTABLE  := main  to  EXECUTABLE := app  // or any name for your application
1
2
3
// launch.json
// edit this line with
"miDebuggerPath": "/usr/bin/gdb"  to  "miDebuggerPath": "/file-path/gcc-arm-8.2-2019.01-x86_64-arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gdb"
10/16/19 - Updated to run with sudo priveleged. Take note on these line#: 41 & 42
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// task.json
// edit this line with
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Build C++ project",
            "type": "shell",
            "command": "make",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "panel": "dedicated",
                "clear": true
            }         
        },
        {
            "label": "Build & run C++ project",
            "dependsOn": [
                "Build C++ project"
            ],
            "type": "shell",
            "command": "sshpass",
            "args": [
                "-p",
                "pi",
                "rsync",
                "-avzP",
                "./bin/*",
                "pi@raspberrypi.local:~/",
                "&&",
                "echo",
                "&&",
                "sshpass",
                "-p",
                "pi",
                "ssh",
                "pi@raspberrypi.local",
                "sudo",
                "'/home/pi/app'"
            ],
            "group": {
                "kind": "test",
                "isDefault": true
            },
            "presentation": {
                "clear": true
            }  
        }
    ]
}
What the above lines do is compile, upload, then remotely run the app in exact order

5). Near the lower right corner of VSCode window, Click on the word linux and select Edit Configurations.... A new json file will be added c_cpp_properties.json.

6). Edit c_cpp_properties.json

1
2
3
// c_cpp_properties.json
// edit this line with
"compilerPath": "/usr/bin/gcc"  to  "compilerPath": "/file-path/gcc-arm-8.2-2019.01-x86_64-arm-linux-gnueabihf/bin/arm-linux-gnueabihf-g++"

7). Press the Build & Run button on the lower left corner of the VSCode window… or press F7 and the result will be printed out on the vscode integrated terminal

Make sure that you are able to ssh into the Pi before running an app or you'll encounter a connection error