Yocto Programming with VSCode: Difference between revisions

From Variscite Wiki
No edit summary
No edit summary
Line 3: Line 3:
--> {{#lst:Yocto_Platform_Customization|{{#var:RELEASE_PARAM|RELEASE_ZEUS_V2.1_VAR-SOM-MX8M-NANO}}}} <!--
--> {{#lst:Yocto_Platform_Customization|{{#var:RELEASE_PARAM|RELEASE_ZEUS_V2.1_VAR-SOM-MX8M-NANO}}}} <!--
--> {{PageHeader|{{#var:HARDWARE_NAME}} - Yocto Programming with Visual Studio Code}} {{DocImage|category1={{#var:HARDWARE_NAME}}|category2=Yocto}}
--> {{PageHeader|{{#var:HARDWARE_NAME}} - Yocto Programming with Visual Studio Code}} {{DocImage|category1={{#var:HARDWARE_NAME}}|category2=Yocto}}
This guide describes how to use Visual Studio Code (VSCode) to develop and debug applications on the {{#var:HARDWARE_NAME}}.
__toc__


= C/C++ Application Development and Debugging using CMake =
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.


VSCode has C/C++ and CMake extensions that make it easy to develop, build, and debug C/C++ applications. Microsoft has a [https://code.visualstudio.com/docs/cpp/cmake-linux detailed guide] for getting started with CMake on Linux.
This guide demonstrates how to create and debug a C++ application using VSCode on the {{#var:HARDWARE_NAME}}.


This section will extend Microsoft's guide to support cross compiling for Variscite IMX8 Yocto images.
__toc__


== Setup Host Computer Environment ==
= Setup Host Computer Environment =


This guide is tested using a fresh Ubuntu 20.04 installation.
This guide is tested using a fresh Ubuntu 20.04 installation.


=== Install Dependencies ===
== Install Dependencies ==


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


=== Install VSCode ===
== Install VSCode ==


  $ sudo snap install --classic code
  $ sudo snap install --classic code


=== Install VSCode Extensions ===
== Install VSCode Extensions ==


VSCode has a graphical interface for installing and managing extensions. To learn more, please see [https://code.visualstudio.com/docs/introvideos/extend Using extensions in Visual Studio Code]
VSCode has a graphical interface for installing and managing extensions. To learn more, please see [https://code.visualstudio.com/docs/introvideos/extend Using extensions in Visual Studio Code]
Line 31: Line 29:
For this guide, we will install the required extensions using the command line:
For this guide, we will install the required extensions using the command line:


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


=== Install Yocto Toolchain ===
== Install Yocto Toolchain ==


A toolchain is necessary for cross compiling applications. To install the toolchain, follow Variscite's {{Varlink2|Yocto Toolchain installation|{{#var:RELEASE_LINK}}}} guide.
A toolchain is necessary for cross compiling applications. To install the toolchain, follow Variscite's {{Varlink2|Yocto Toolchain installation|{{#var:RELEASE_LINK}}}} guide.


== Build a "Hello World" CMake project using VSCode ==
= Create, cross compile, and run a new "Hello World" project =


Create an empty project directory and open VSCode:
First, open a new terminal and configure the environment with the toolchain setup script:
 
$ source {{#var:TOOLCHAIN_LOCATION}}
 
From the same terminal, create an empty project directory and open VSCode:


  $ mkdir ~/var-hello-world
  $ mkdir ~/var-hello-world
  $ cd ~/var-hello-world
  $ cd ~/var-hello-world
$ source {{#var:TOOLCHAIN_LOCATION}}
  $ code .
  $ code .


Open the VSCode Command Palette (<code>Ctrl+Shift+P</code>) and run the <code>CMake: Quick Start</code> command.
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 <code>ctrl+shift+`</code>. Your workspace should look similar to this:
 
[[File:vscode-gdb-build.png|800px]]


Enter a project name and select 'Create an Executable'. This will create a new "Hello World" CMake project.
Run <code>make</code> in the new terminal


Next, select a kit informing VSCode where the toolchain compiler is. Open the Command Palette (<code>Ctrl+Shift+P</code>) and run the <code>CMake: Select a kit</code> command and select <code>{{#var:TOOLCHAIN_TYPE}}</code>
$ make


Open the Command Palette (<code>Ctrl+Shift+P</code>) and run the <code>CMake: Build</code> command
Send the file to the target device using SCP:


Deploy the binary to the target device:
$ scp hello.bin root@<ip addr>:/home/root/


$ scp build/hello root@192.168.0.174:
Run <code>hello.bin</code> on the target device:


Run the binary on the target device:
# ./hello.bin
Hello, World!


# ./hello
= Debug the "Hello World" project =
Hello, world!

Revision as of 23:55, 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

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!

Debug the "Hello World" project