Skip to main content
Version: BSP 7.x.y

Hello World in C/C++

Introduction

This article provides step-by-step instructions on how to cross-compile a simple Hello World application on the development host through command-line or by using an IDE. It also gives instructions on how to run the application on the target device.

Follow the Embedded Linux environment setup article for information on setting up your environment for Embedded Linux application development.

Toolchain/SDK

To compile an application for the SoM, first we need a Toolchain that can generate ARM binaries and link them to libraries present in the SoM Operating System.

Use BitBake to build and install the Software Development Kit (SDK) for your SoM, as described in the Linux SDKs article. The SDK comes with a toolchain for the SoM and other important elements such as the sysroot.

Application

info

In this example, the project path is ~/Documents/embedded/hello-world-c/.

The following steps guide you through the creation of the classic Hello World application in C:

  1. Create a directory for your project:
mkdir -p ~/Documents/embedded/hello-world-c/
  1. cd to the project folder:
cd ~/Documents/embedded/hello-world-c/
  1. Create the hello_world.c file in the project directory, then paste the Hello World code:
#include <stdio.h>

int main() {
printf("Hello World!\n");
return 0;
}

Compilation

  1. source the environment-setup script from SDK section.
    Assuming the SDK is installed in the default location (/opt/<distro>/<version>/environment-setup-...):
source /opt/tdx-xwayland/6.3.0/environment-setup-...
  1. Use the SDK toolchain to build the application:
$CC $CFLAGS hello_world.c -o hello_world
info

The environment-setup script exports variables such as:

  • $CC: The C compiler.
  • $CFLAGS: The C build flags.
  • $CXX: The C++ compiler.
  • $CXXFLAGS: The C++ build flags.
  • $LDFLAGS: The linker flags.

And also provides a pkg-config tool.

A binary executable named hello_world should appear in the directory.

Execution

  1. Copy the application to the SoM using tools such as scp, sftp, or use a USB stick.
  2. Connect to the module, either by ssh or by serial.
  3. cd to the hello_world application directory.
  4. Run the application:
./hello_world

A message showing Hello World! should appear in your terminal.

info

If you try to run the program on a x86 machine, the system will show an exec format error:

bash: ./hello_world: cannot execute binary file: Exec format error

CMake

BitBake's populate_sdk command can be configured to add CMake support to the SDK which also configures the environment-setup script to set CMake options making cross compilation transparent to the user.

SDK support

Add CMake support to your SDK (see SDK section) by adding the following line to your build/conf/local.conf file:

TOOLCHAIN_HOST_TASK:append = " nativesdk-cmake"

Then re-build and install your new SDK.

Project Configuration

Create the CMakeLists.txt file in your C project directory, then paste the following code:

cmake_minimum_required(VERSION 3.21)

project(
CMakeHelloWorld
VERSION 1.0
LANGUAGES C)

add_executable(cmake_hello_world hello_world.c)

Compilation

In your project directory:

  1. Create the build folder:
mkdir -p ~/Documents/embedded/hello-world-c/build/
  1. Change directory to the build folder:
cd ~/Documents/embedded/hello-world-c/build/
  1. Source the environment script from the new SDK, to load tools such as pkg-config and CMake environment configuration.
    Assuming the SDK is installed in the default location (/opt/<distro>/<version>/environment-setup-...):
source /opt/tdx-xwayland/6.3.0/environment-setup-...
  1. Run CMake to generate the makefile:
cmake ..
  1. Run make to build the application:
make

A binary executable named cmake_hello_world should appear in the directory.

Follow the instructions from Execution section on how to send the executable to the SoM and run the application.

IDE integration

Some popular IDEs allow using it as the build system for C/C++ projects:

  • CLion
  • KDevelop
  • QtCreator
  • Visual Studio

To be able to cross-compile, you may need to configure your IDE to source the environment-setup script. A list of IDEs that support CMake is available in CMake documentation.



Send Feedback!