Template:ArticleMainWriter Template:ArticleProposedVersion
1. Overview[edit source]
This stage will explain you how to create, build and execute a simple C code application using the freshly installed SDK.
2. Code[edit source]
- Create your directory that will host your source codes
mkdir $HOME/STM32MPU_workspace/STM32MP15-Ecosystem-v1.0.0/Developer-Package/sources
- Create a directory for this user space example
mkdir $HOME/STM32MPU_workspace/STM32MP15-Ecosystem-v1.0.0/Developer-Package/sources/gtk_hello_world_example cd $HOME/STM32MPU_workspace/STM32MP15-Ecosystem-v1.0.0/Developer-Package/sources/gtk_hello_world_example
- Create the source code file for this user space example: hello_world_example.c
// SPDX-identifier: GPL-2.0 /* * Copyright (C) STMicroelectronics SA 2018 * * Authors: Jean-Christophe Trotin <jean-christophe.trotin@st.com> * */ #include <stdio.h> #include <unistd.h> int main(int argc, char **argv) { int i =11; printf("\nUser space example: hello world from STMicroelectronics\n"); setbuf(stdout,NULL); while (i--) { printf("%i ", i); sleep(1); } printf("\nUser space example: goodbye from STMicroelectronics\n"); return(0); }
3. Build[edit source]
Three ways to use the OpenSTLinux SDK to cross-compile this user space example are proposed below: (1) command line (2) makefile-based project (3) autotools-based project.
3.1. Command line[edit source]
This method allows quick cross-compilation of a single-source code file. It applies if the project has only one file.
The cross-development toolchain is associated with the sysroot that contains the header files and libraries needed for generating binaries that run on the target architecture (see SDK for OpenSTLinux distribution#Native and target sysroots).
The sysroot location is specified with the --sysroot option.
The sysroot location must be specified using the --sysroot option. The CC environment variable created by the SDK already includes the --sysroot option that points to the SDK sysroot location.
echo $CC
arm-openstlinux_weston-linux-gnueabi-gcc -march=armv7ve -marm -mfpu=neon-vfpv4 -mfloat-abi=hard -mcpu=cortex-a7 --sysroot=/local/SDK/2.4+openstlinux-4.14-rocko-mp1-18-07-03/sysroots/cortexa7hf-neon-vfpv4-openstlinux_weston-linux-gnueabi
- Create the directory in which the generated binary is to be stored
mkdir -p install_artifact install_artifact/usr install_artifact/usr/local install_artifact/usr/local/bin
- Cross-compile the single source code file for the user space example
$CC hello_world_example.c -o ./install_artifact/usr/local/bin/hello_world_example
3.2. Makefile-based project[edit source]
For this method, the cross-toolchain environment variables established by running the cross-toolchain environment setup script are subject to general make rules.
For example, see the following environment variables:
echo $CC arm-openstlinux_weston-linux-gnueabi-gcc -march=armv7ve -marm -mfpu=neon-vfpv4 -mfloat-abi=hard -mcpu=cortex-a7 --sysroot=/local/SDK/2.4+openstlinux-4.14-rocko-mp1-18-07-03/sysroots/cortexa7hf-neon-vfpv4-openstlinux_weston-linux-gnueabi echo $CFLAGS -O2 -pipe -g -feliminate-unused-debug-types echo $LDFLAGS -Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed echo $LD arm-openstlinux_weston-linux-gnueabi-ld --sysroot=/local/SDK/2.4+openstlinux-4.14-rocko-mp1-18-07-03/sysroots/cortexa7hf-neon-vfpv4-openstlinux_weston-linux-gnueabi
- Create the makefile for this user space example: Makefile
PROG = hello_world_example SRCS = hello_world_example.c OBJS = $(SRCS:.c=.o) CLEANFILES = $(PROG) INSTALL_DIR = ./install_artifact/usr/local/bin # Add / change option in CFLAGS if needed # CFLAGS += <new option> $(PROG): $(OBJS) $(CC) $(CFLAGS) -o $(PROG) $(OBJS) .c.o: $(CC) $(CFLAGS) -c $< -o $@ all: $(PROG) clean: rm -f $(CLEANFILES) $(patsubst %.c,%.o, $(SRCS)) *~ install: $(PROG) mkdir -p $(INSTALL_DIR) install $(PROG) $(INSTALL_DIR)
- Cross-compile the project
make make install
4. Deploy and execute[edit source]
- Check that the generated binary for this user space example is in: ./install_artifact/usr/local/bin/hello_world_example
- Push this binary onto the board
scp -r install_artifact/* root@<board ip address>:/
- Execute this user space example
cd /usr/local/bin ./hello_world_example User space example: hello world from STMicroelectronics 10 9 8 7 6 5 4 3 2 1 0 User space example: goodbye from STMicroelectronics