Yocto Programming with VSCode: Difference between revisions

From Variscite Wiki
No edit summary
No edit summary
Line 84: Line 84:


= Remote Debugging with VSCode =
= Remote Debugging with VSCode =
= Example Project Source Code =

Revision as of 22:57, 13 July 2021

VAR-SOM-MX8M-NANO - Yocto Programming with Visual Studio Code

Visual Studio Code (VSCode) is a powerful, modern code editor that can be used to develop and debug C/C++ applications on Variscite System on Modules.

This guide demonstrates how to create and debug a C++ application using VSCode on the VAR-SOM-MX8M-NANO.

Setup Host Computer Environment

This guide is tested using a fresh Ubuntu 20.04 installation.

Install Dependencies

$ sudo apt-get -y update
$ sudo apt-get -y install build-essential gdb gdb-multiarch cmake

Install VSCode

$ sudo snap install --classic code

Install VSCode Extensions

VSCode has a graphical interface for installing and managing extensions. To learn more, please see Using extensions in Visual Studio Code

For this guide, we will install the required extensions using the command line:

$ code --install-extension ms-vscode.cpptools

Install Yocto Toolchain

A toolchain is necessary for cross compiling applications. To install the toolchain, follow Variscite's Yocto Toolchain installation guide.

Create, cross compile, and run a new "Hello World" project

First, open a new terminal and configure the environment with the toolchain setup script:

$ source /opt/fsl-imx-xwayland/5.4-zeus/environment-setup-aarch64-poky-linux 

From the same terminal, create an empty project directory and open VSCode:

$ mkdir ~/var-hello-world
$ cd ~/var-hello-world
$ code .

Create a new file called main.cpp. Since we will be debugging remotely using GDB, use freopen to redirect stdout to /dev/console on the target device:

#include <iostream>

int main(int argc, char *argv[]) {
    FILE *fp;
    fp = freopen("/dev/console","w",stdout);
    printf("Hello, World!\n");
    fclose(fp);
    return 0;
}

Create a new Makefile to build hello.bin

all: main.cpp
	$(CXX) $(CXXFLAGS) main.cpp -g -o hello.bin
clean:
	rm -f hello.bin

Open a new terminal in VSCode by typing ctrl+shift+`. Your workspace should look similar to this:

Vscode-gdb-build.png

Run make in the new terminal

$ make

Send the file to the target device using SCP:

$ scp hello.bin root@<ip addr>:/home/root/

Run hello.bin on the target device:

# ./hello.bin 
Hello, World!

Remote Debugging with VSCode

Example Project Source Code