sábado, 3 de maio de 2014

First software for Embedded Linux

In Article C Language for Embedded Linux was shown forms of programming for embedded Linux systems. And also already explained What is Embedded Linux.

In this article we will show how to program a software for Embedded Linux and what is needed for this. The example to be presented runs on any electronic board Linux, for example, BEAGLEBONE, Raspberry pi and other. However, the tests will be done on board Olimex A13-OLinuxino. And the host system can be any current GNU/Linux distribution: Debian, Ubuntu, Fedora ...


Installing and configuring the cross-toolchain


If you have questions about what is cross-toolchain read the article What is Embedded Linux. The cross-toolchain from Linaro will be used for example Linaro gcc-arm-linux-4.7-2013.04-gnueabihf-20130415, being stable and run on virtually any distribution of your choice.

1 - Type the following command to download it:
$ wget https://releases.linaro.org/13.04/components/toolchain/binaries/gcc-linaro-arm-linux-gnueabihf-4.7-2013.04-20130415_linux.tar.xz

2 - Extract the compiler:
$ tar -xJf gcc-linaro-arm-linux-gnueabihf-4.7-2013.04-20130415_linux.tar.xz

3 - Create a folder called toolchains/eabi under /opt:
# mkdir /opt/toolchains
# mkdir /opt/toolchains/eabi

4 - Move the compiler to the folder /opt/toolchains/eabi:
# mv gcc-linaro-arm-linux-gnueabihf-4.7-2013.04-20130415_linux /opt/toolchains/eabi/

5 - Add the directory compiler to your PATH:
$ mcedit .bashrc (use your favorite text editor)

And add to the last line:
export PATH="/opt/toolchains/eabi/gcc-linaro-arm-linux-gnueabihf-4.7-2013.04-20130415_linux/bin/:$PATH"

6 - Test the basic operation (before doing the test, close and open the terminal again):
arm-linux-gnueabihf-gcc -v
You should see in the last line:
gcc version 4.7.3 20130328 (prerelease) (crosstool-NG linaro-1.13.1-4.7-2013.04-20130415 - Linaro GCC 2013.04)

Code Example: Hello World

#include 
int main()
{
   printf(“Hello\n”);
   return 0;
}
Copy and save as hello.c in your working folder.

Code compilation


Type the following command:
$ arm-linux-gnueabihf-gcc hello.c -o hello

If the compiler does not generate any error, it worked! Make sure the file is generated for ARM:
$ file hello
The output should be:
hello: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.31, BuildID[sha1]=d3db9d8be9c58c465aab540c81e0cdc5f7c63551, not stripped


Transfer to board


The transfer of the file to board can be done in several ways. If you run Linux from an SD card copying to the card. Here we will use the remote transfer by SSH. Then just type:
$ scp hello user@192.168.1.x:/some_folder
Change the user, ip and folder according to your settings and according to your network.

Testing code


So to test, type in Linux board:
$ ./hello

Using Makefiles to Compile Your Application


We will compile the same code, but now we will use a Makefile to automate the build process. From now on, we will always use the Makefile to build our projects.
PROG = hello
FILE = hello.c

SRCS = $(FILE)

OPT=-Wall -g -I../include
#LIBS=-L../lib -lpthread -ludev -lvlc

CC= arm-linux-gnueabihf-gcc


all:
 $(CC) $(OPT) $(SRCS) -o $(PROG) $(LIBS)

 mv $(PROG) ../bin

clean:
 rm -rf ../bin/*

Copy this code and save as Makefile. The line below the directives all and clean must have a tab to work, otherwise there will be errors! If I have problems with the Makefile, download here: Makefike. Enter the command:
$ make

If no error is shown then everything is working. We will not use libraries at the moment, so let LIBS commented!

Ready! This is all you need to compile any simple software for Embedded Linux. We will see other posts using dynamically linked libraries for more complex projects.

References

Nenhum comentário:

Postar um comentário