Personal tools

Difference between revisions of "Building simple C program"

From SomLabs Wiki

Jump to: navigation, search
(Created page with "{{PageHeaderBuilding simple C program}} __toc__ == On target system == One of few ways to create applications is to connect to target board via UART-USB converter or SSH cli...")
(No difference)

Revision as of 13:00, 6 November 2019

Template:PageHeaderBuilding simple C program

On target system

One of few ways to create applications is to connect to target board via UART-USB converter or SSH client, use a text editor (for example nano) to write code and compile them with gcc. To do that install compiler and some additional software:

root@somlabs:~# apt install build-essential

Now create your program file with nano editor:

root@somlabs:~# nano HelloWorld.c

Enter the code below to the file:

#include <stdio.h>

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

Save the file (Ctrl+O) and exit (Ctrl+X). Now you can compile and run your program:

root@somlabs:~# gcc -o HelloWorld HelloWorld.c
root@somlabs:~# ./HelloWorld
Hello world!
root@somlabs:~#

On Linux host

Other method is to write your application on host system. In this case Ubuntu 18.04 will be used with the Linaro toolchain.

Download and extract the toolchain:

wget https://releases.linaro.org/components/toolchain/binaries/7.4-2019.02/arm-linux-gnueabihf/gcc-linaro-7.4.1-2019.02-x86_64_arm-linux-gnueabihf.tar.xz
tar -xf gcc-linaro-7.4.1-2019.02-x86_64_arm-linux-gnueabihf.tar.xz

Export the path to the compiler:

export PATH=$PATH:<KERNEL_PATCH_PATH>/gcc-linaro-7.4.1-2019.02-x86_64_arm-linux-gnueabihf/bin

Now you are ready to create basic application on your host, first steps are just like in case of development on target, but you need to perform them on your host: Create your program file with nano editor:

nano HelloWorld.c

Enter the code below to the file:

#include <stdio.h>

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

Save the file (Ctrl+O) and exit (Ctrl+X). Now you can compile and run your program:

arm-linux-gnueabihf-gcc -o HelloWorld HelloWorld.c

The created executable can be run only on ARM architecture - it can be verified by calling the command:

file HelloWorld

The output contains the details about the binary:

HelloWorld: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-, for GNU/Linux 3.2.0, BuildID[sha1]=f34ae20591d1c22fdcfcaa8f6aee9568f9b250e4, with debug_info, not stripped

Now it is time to upload executable file to the target (you need to find it's IP before, for example by running ip a command on target and allow access to the target using ssh protocol):

scp HelloWorld root@192.168.0.53:/root

Now your executable should be in /root directory of the target system, you can run it (enter the command below on target):

root@somlabs:~# ./HelloWorld
Hello world!
root@somlabs:~#
NXP Partner ST Partner Renesas Partner