Cross-compiling
Wikipedia defines a toolchain as "the set of computer programs (tools) that are used to create a product (typically another computer program or system of programs). The tools may be used in a chain, so that the output of each tool becomes the input for the next, but the term is used widely to refer to any set of linked development tools."If the toolchain used creates binary programs for a machine architecture other than the host architecture, it is termed a cross-compiler.
Contents
- Sample source code (helloworld.c)
- Cross-compiling the source
- Running your program
- Automating the build with GNU make
Sample source code (helloworld.c)
We'll using a trivial code example to illustrate the cross-compilation process. Create a file called helloworld.c containing the following code
#include <stdio.h> int main(int argc, char **argv) { printf("Hello world!\n"); return 0; }
Cross-compiling the source
Now we'll cross-compile this code so that it will run on the embedded controller. To keep things neat we'll first create a variable pointing at the base directory of the toolchain (in this case we're using a Xscale toolchain in /opt/devbot. This might be different for you).
$ export CCROOT=/opt/devbot/armxscale-uclibc
then invoke the cross-compiler with
$ $CCROOT/bin/arm-linux-uclibc-gcc -I $CCROOT/include -L $CCROOT/lib -o helloworld helloworld.c
The -I and -L flags tell the compiler where to look for headers and libraries respectively. You can check that this has worked by using the standard GNU/Linux file-type detection command file e.g.
$ file helloworld helloworld: ELF 32-bit LSB executable, ARM, version 1 (ARM), dynamically linked (uses shared libs), not stripped
Running your program
Since the program is cross-compiled you'll need to transfer it to the module to test. scp is the recommended way to transfer files
scp helloworld root@$IP_TARGET:
Automating the build with GNU make
The GNU make tool is commonly used to automate the build process. Full documentation for make can be be found at:http://www.gnu.org/software/make/manual/make.html
To get you started here is a sample Makefile for the "helloworld" project. Note that this automatically adds support for the DevBot library and its dependencies (glib).
################################################################################ # # Example Makefile for UoE # ################################################################################ # Insert the correct toolchain details here CCROOT=/opt/devbot/armxscale-uclibc CC=$(CCROOT)/bin/arm-linux-gcc # The name of the program PROGRAM=helloworld # Objects used by the program (one per .c file) OBJS=helloworld.o # Additional libraries used by the program LIBS= # Additional C flags CFLAGS=-Werror # Drivers DRIVER_OBJS= ################################################################################ # # It should not be necessary to alter the following definitions # ################################################################################ $(PROGRAM): $(OBJS) $(CC) $(LDFLAGS) -o helloworld $(OBJS) $(DRIVER_OBJS) %.o:%.c $(CC) $(CFLAGS) -c $^ CFLAGS+=-I $(CCROOT)/include LDFLAGS=-L$(CCROOT)/lib LDFLAGS+=$(patsubst %,-l%,$(LIBS)) # Devbot libraries/drivers LDFLAGS+=-L$(CCROOT)/lib/devbot -lbot # glib-2.0 LDFLAGS+=-lglib-2.0 CFLAGS+=-I$(CCROOT)/include/glib-2.0 -I$(CCROOT)/lib/glib-2.0/include
(c) 2006 Edinburgh Robotics Ltd.
Generated on Fri Feb 2 11:24:07 2007 for libbot by doxygen 1.5.1