Archive for the ‘Programming’ Category

4
Mar

Simple Cross Compile Example

The following is a very introductory guide to cross compiling a simple C++ program targeted towards an ARM platform. Development environments differ greatly, but for the most part, they all follow similar steps like outlined below. The very first step is to obtain a cross compile toolchain. CodeSourcery has a free, lite gcc toolchain available for download (as mentioned in a previous post). Once you download an appropriate toolchain for your target, you’ll need to extract it on your development host machine. You’ll notice a directory structure similar to opt/crosstool/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu/bin/arm-unknown-linux-gnu- which is what we’ll use here.
[Read more →]

16
Feb

CodeSourcery.com: A Place to Get Cross Compile Toolchains for ARM

This is something that I’m still coming to grips with, but today I learned that the cross compile toolchains that Technologic Systems provides for at least some of their products come from CodeSourcery.com. A quick blurb from their website:

CodeSourcery, in partnership with ARM, Ltd., develops improvements to the GNU Toolchain for ARM processors and provides regular, validated releases of the GNU Toolchain. Sourcery G++ Lite Edition supports ARM, Thumb, and Thumb-2 compilation for all architectures in active use, including Version 7 of the ARM Architecture.

For example, today I needed a newer version of the g++ cross compiler targeted towards EABI and glibc, so I took a look around the website at http://www.codesourcery.com/sgpp/lite/arm and found the release in the downloads section here: http://www.codesourcery.com/sgpp/lite/arm/portal/[email protected]=lite

A short term goal for myself is to figure out what goes into creating a cross compile environment. I think this would be a valuable skill in the embedded Linux market. For now, this was a shock to learn about how simple it really is to be able to simply download the toolchain and that there wasn’t any other trickery or knowledge required.

19
Jan

My First Makefile for Compiling GTK+ (Or Any) Apps

A Makefile provides an easy, fast way to compile an application that requires more than just a simple gcc myprogram.c -o myprogram command. For example, to compile a GTK+ app, the common, basic compile command is gcc -g -Wall myguiapp.c -o myguiapp -export-dynamic `pkg-config –cflags –libs gtk+-2.0`. It’s not terrible, but it would be a pain to have to retype that every time you need to recompile. The alternative is to create a Makefile and type make. This is my very first Makefile, and I believe it makes a great example:

NAME=myguiapp
CFLAGS=-g -Wall -o $(NAME)
GTKFLAGS=-export-dynamic `pkg-config --cflags --libs gtk+-2.0`
SRCS=main.c
CC=gcc

# top-level rule to create the program.
all: main

# compiling the source file.
main: $(SRCS)
 $(CC) $(CFLAGS) $(SRCS) $(GTKFLAGS)

# cleaning everything that can be automatically recreated with "make".
clean:
 /bin/rm -f $(NAME)

As you can see, when make is called, it will compile the source with all my options including the program name. When make clean is called, it will delete the compiled program.

One important thing to remember is that these Makefiles are very picky about spacing and tabs.  You MUST use a TAB at the beginning of commands and a TAB must not be at the beginning of blank lines. The first error causes the commands not to run. The second causes the “make” utility to complain that there is a “blank” command.

Obviously, this is an incredibly simple Makefile in comparison to a lot of them out there, but this is a good starting point. You can customize the Makefile to match your compiling needs. If you’d like to learn more, Google is your friend. I can point you in a couple of good directions with these links:
http://mrbook.org/tutorials/make/
http://www.cs.umd.edu/class/fall2002/cmsc214/Tutorial/makefile.html

Happy coding!

13
Jan

GCC’s Preprocessor Option ‘-D’ and Example Usage with #ifdef

What I’ve Learned Today:

Say that you have a program that you’d like to be able to compile for both ARM and x86 architectures, but you have portions of the code written in assembly language.  Obviously, assembly code written for an ARM CPU won’t compile and run on an x86 CPU, so how would you cleanly solve this problem without writing two different programs?  One suggestion would be to use #ifndef (see this page for an explanation) and the ‘-D’ flag when invoking the gcc compiler to tell the compiler to ignore a particular block of code.  Here is an example snippet of code that you might want to use in a program:

#ifndef X86
   // This is ARM assembly code which is preferred architecture
   asm(
      "mov     r0, r0nt"
      "mov     r0, r0nt"
      "mov     r0, r0nt"
      "mov     r0, r0"
   );
#else
   // This is X86 assembly code
   asm(
      "movl %eax, %ebxnt"
      "movl $56, %esint"
      "movl %ecx, $label(%edx,%ebx,$4)nt"
      "movb %ah, (%ebx)"
   );
#endif

This program will be developed mostly for the ARM architecture, but we would like to be able to easily compile it for x86 as well. When we do want to compile for x86, we will use the command:

gcc -D X86 myprogram.c -o myprogram_x86

The -D flag is a preprocessor option which, in my understanding, sets a sort of #define for the program before compiling (see ‘man gcc‘ page). The preprocessor will check for any of the #ifndef or #ifdef lines of the program and then act accordingly. In this example, the lines between “#ifndef X86” and “#else” containing the ARM assembly code will be discarded and the lines between “#else” and “#endif” containing the x86 assembly code will be used.

« Previous Page