Build and Run your First C/C++ Application
Overview
When it comes to Linux development, it is possible to choose several IDEs, workflows and programming languages for development. Aiming to simplify your development experience Toradex provides TorizonCore IDE integration with Visual Studio Code for Windows and Linux PCs, and Visual Studio for Windows PCs.
In this module you will:
- Use the Visual Studio IDE.
- Cross-compile your application using the Torizon Extension for Visual Studio.
- Deploy it to the target module and debug it remotely from your development PC.
Support for a more complex workflow on Visual Studio will be presented to you at the end of the quickstart guide, as well as support for other programming languages on Visual Studio Code. If you have a Linux PC for development, you can also try the quickstart guide for Torizon with Linux selected as Development PC OS.
Typographic Conventions
Throughout the Toradex documentation, the following typographic conventions are used:
$ (dollar sign) Command in the host computer (e.g. your PC)
$ Command in your PC
$$ (double dollar sign) Command in a container in the host computer (e.g. your PC)
$$ Command inside a container in your PC
# (hashtag) Command in the target device/board (e.g. Linux terminal)
# Command in the target board, e.g. Colibri iMX6
## (double hashtag) Command inside a container in the target device (Torizon)
## Command inside a container in Torizon
> (greater-than sign) Command in the bootloader (e.g. U-Boot console)
> Command in the Bootloader
No symbol: Command output
$ Command waiting for output
Output
Prerequisites
For this Quickstart Guide:
- Host machine with Windows 10 version 2004 (a.k.a. May 2020 update) or higher.
- Successfully completed the previous lesson from this guide.
For this lesson:
- Visual Studio 2019.
- Toradex hardware and computer on the same network.
Carefully read this module's cover page clicking on "Module 2: First Steps with Torizon" on the left menu bar before starting this lesson.
Step 1
When installing Visual Studio make sure you select at least the following workloads for the correct operation of the Torizon Extension for Visual Studio.
If you already have Visual Studio installed, run the Visual Studio Installer and modify your current setup:
Step 2
The latest versions of the Toradex Torizon Support extension can be found on the Visual Studio marketplace, in two versions:
- Toradex Torizon C/C++ Support: stable version, with a slower release cycle.
- Toradex Torizon C/C++ Support (Early Access): preview version, always gets the latest features but is more subject to bugs.
Close all running instances of Visual Studio. Choose one of Torizon extensions from the above, download and install it.
The extension is available for Visual Studio 2019 only!
Check that the installation was successful by opening the extension list under Extensions > Manage Extensions:
Step 3
Select Tools > Download/update base and sdk containers:
Be patient! This process can take a while.
Step 4
The extension provides some windows and toolbars but they are hidden by default. You can show the device list by selecting View > Other Windows > Torizon devices from the menu bar in Visual Studio.
Once it shows up, you can detect devices through serial port or network. In this guide we will use the network device detection.
Step 5
To add your device to this list, select Detect device on the network and set your board's IP or hostname as found out on the previous lessons:
Remember that the credentials are username: torizon
and password: <custom password you have set during the first login>
As soon as you click Detect, the board will reboot! All instances of Putty will close with a Connection Lost message.
Step 6
After the board reboots, refresh the device on the list. The Windows Defender will ask you to allow moses.exe
.
Moses is the Torizon Extension back-end. Click on Allow Access.
Step 7
See the collected data from the board:
Step 8
Enable the Torizon toolbar on Visual Studio selecting Tools > Customize, scroll down the list, select "Torizon Toolbar" and hit Close:
Step 9
Create a New Project and select Torizon C/C++ Application from the Visual C++ category:
Step 10
Give a Project Name to your project and make sure to mark the checkbox Place solution and project in the same directory:
Step 11
When you click Create in the previous step, a dialog will ask you to Select supported platforms.
Select debian arm64v8 bullseye
and verify that torizon is the username:
Please notice that the list of platforms may change. New updates of the extension will make additional platforms available with new releases of debian and specialized platforms with specific libraries pre-installed.
Step 12
Open Debug > Linux Console so you can see the application output.
Step 13
On the Solution Explorer menu open the main.cpp file and slightly modify it to make our debug fun:
#include <cstdio>
int main() {
int var = 1, i;
for(i = 0; i < 5; i++){
var = 2*var;
printf("Loop %d - var=%d\n", i, var);
}
return 0;
}
Step 14
Add a breakpoint by clicking on the left of the line number. For instance, insert a breakpoint relative to the printf
command (line 7).
Step 15
Either choose Debug > Deploy and debug Torizon or click on Deploy and Debug Torizon Application on the Torizon Toolbar:
A dialog will ask if you want to build the application. Choose Yes:
This step takes a while to finish since the app has to be built.
See the expected output in the animated illustration below:
Step 16
When the build is finished, deploy the application to the computer on module by selecting Debug > Deploy and debug Torizon and selecting your board:
This step takes a while, when run for the first time, since the container has to be built and deployed to the target.
Step 17
You can use the Visual Studio debugging features to step through your code and observe variable values, functions, etc.
Takeaway
As the Quickstart is a step-by-step guide, the current lesson is only a brief introduction to application development with Torizon. For a great development experience, you can choose to use the Visual Studio Extension For Torizon and write your application in C/C++ or use the Visual Studio Code Extension for Torizon and write your application in C/C++, C#/.NET or Python.
You can also use other programming languages, or even ready-to-use technologies such as databases, by either choosing a container image from the community as the official Node.js container image or building your own container based on a community one or our Debian Containers for Torizon.
FAQ
Where do I find more information about the Torizon Extension for Visual Studio?
Check out the article Visual Studio Extension for Torizon.
Something went wrong while following the steps from this lesson. How do I proceed?
You can report your issue through the Toradex Community.