2. Create and build a "Hello world" application
Follow these steps to create and build a "Hello world" application:
- Create a directory called hello_world:
- Inside the hello_world directory, create a file named hello-world.c:
- Edit the hello-world.c file with the following contents:
- Create a makefile file inside the hello_world directory:
- Edit the file Makefile with the following contents:
- Before building the code, export the toolchain environment:
- Cross-compile the code using make.
~> mkdir hello_world
~> cd hello_world ~> touch hello-world.c
hello-world.c
#include <stdio.h> #include <unistd.h> int main(int argc, char *argv[]){ int i; for (i = 1; i <= 10; i++) { printf("%d - Hello world!\n", i); sleep(1); } return 0; }
~> touch Makefile
Makefile
BINARY := hello-world CFLAGS += -Wall -O0 .PHONY: all all: $(BINARY) $(BINARY): hello-world.o .PHONY: clean clean: -rm -f *.o $(BINARY)
~> . /opt/dey/2.2-r3/environment-setup-cortexa7hf-neon-dey-linux-gnueabi
~> make
Note If you are building a Qt application, run qmake before cross-compiling:
~> qmake
The binary file, hello-world, is generated inside the directory hello_world.
~> ls -l total 28 -rwxrwxr-x 1 user user 12084 dic 18 12:39 hello-world -rw-rw-r-- 1 user user 199 dic 18 12:39 hello-world.c -rw-rw-r-- 1 user user 4968 dic 18 12:39 hello-world.o -rw-rw-r-- 1 user user 189 dic 18 12:39 Makefile