Skip navigation
TechXchange
Digi-Key TechXchange Communities > Projects and Designs > Discussions
179589 Views 65 Replies Latest reply: May 16, 2011 4:39 PM by Robert Nelson RSS
Currently Being Moderated

Apr 26, 2011 11:33 AM

Please help...

Can any one please help he how to compile a C program on the ARM processor ??

 

Thank you very much in advance,

 

Regards,

Timothy

  • Robert Nelson designspecialist-micro 70 posts since
    Mar 21, 2011
    Currently Being Moderated
    Apr 26, 2011 12:16 PM (in response to Timothy)
    Re: Please help...

    Hi Timothy,

     

    Can you please expand on your question.

    compile a C program on the ARM processor ??

     

    Are we talking about Natively Compiling a C program directly on an ARM processor, or Cross-Compiling a C program from an x86/etc and copying the ARM binary to the ARM processor.

     

    Are we talking bare-metal or os (linux/qnx) based system?

     

    What processor are you currently targeting with your design? (with or without mmu/etc)

     

    Regards,

      • Robert Nelson designspecialist-micro 70 posts since
        Mar 21, 2011
        Currently Being Moderated
        Apr 26, 2011 9:38 PM (in response to Timothy)
        Re: Please help...

        Hi Timothy,

         

        What your describing is actually pretty straight forward to do if done carefully...  Take Debian for example, the same base OS source built on 8 different architectures including ARM in the current stable release.

         

        But there are still small subtle differences that you must watch out for.  So for starters of the many "ARM" processor's which one are you actually targeting? (LPCxx/SAMxx/OMAPxx)?  Also is it already running an OS (such as Linux) or just a bare-metal application?

         

        Regards,

          • Robert Nelson designspecialist-micro 70 posts since
            Mar 21, 2011
            Currently Being Moderated
            Apr 27, 2011 7:17 AM (in response to Timothy)
            Re: Please help...

            Hi Timothy,

             

            Since your using the Hawkboard, I'm going to assume your either running Angstrom, Debian or the old Ubuntu Jaunty release.

             

            To build your C program for ARM, you going to need a Cross Compiler..

             

            If your running Angstrom, did you build Angstrom from their git tree? (it builds it's own Cross Compiler).

             

            Otherwise download and install Code Soucery's gcc for arm: http://www.codesourcery.com/sgpp/lite/arm/portal/release1600

             

            Next, in your makefile edit the gcc/ld/as calls with a new prefix: "CROSS_COMPILE=arm-linux-gnueabi-" making sure to include the path where apprioprate.

             

            Assuming your Makefile has Something like this:

            

            AS              = $(CROSS_COMPILE)as

            LD              = $(CROSS_COMPILE)ld

            CC              = $(CROSS_COMPILE)gcc

            CPP             = $(CC) -E

            AR              = $(CROSS_COMPILE)ar

            NM              = $(CROSS_COMPILE)nm

            STRIP           = $(CROSS_COMPILE)strip

            OBJCOPY         = $(CROSS_COMPILE)objcopy

            OBJDUMP         = $(CROSS_COMPILE)objdump

             

            Regards,

              • Robert Nelson designspecialist-micro 70 posts since
                Mar 21, 2011
                Currently Being Moderated
                Apr 27, 2011 7:28 AM (in response to Timothy)
                Re: Please help...

                Hi Timothy,

                 

                The gcc compiler in Ubuntu is targeted for x86.. You need a Cross Compiler to build an ARM binary, please follow the link in the previous message to download a ARM cross compiler.  Once downloaded and installed, you'll need to edit your Makefile like shown in the previous message.

                 

                Regards,

                    • Robert Nelson designspecialist-micro 70 posts since
                      Mar 21, 2011
                      Currently Being Moderated
                      Apr 27, 2011 12:57 PM (in response to Timothy)
                      Re: Please help...

                      I just use the "IA32 GNU/Linux TAR" package, and un-tar it next to the older release's...

                       

                      Regards,

                          • Robert Nelson designspecialist-micro 70 posts since
                            Mar 21, 2011
                            Currently Being Moderated
                            Apr 28, 2011 8:25 AM (in response to Timothy)
                            Re: Please help...

                            Hi Timothy,

                             

                            It's a shell script, it just needs to be set as excutable (sudo chmod +x *filename*) and run, there is no application to open it.

                             

                            But like I mentioned in my previous message, I don't recommend the binary installer.  It modifies the system path and therefore makes it hard to run mutliple versions of any other Cross Comipler. Use the tar package: IA32 GNU/Linux TAR...

                             

                             

                            From your home directory:

                             

                            voodoo@voodoo-e6400:~$ tar xjf Downloads/arm-2010.09-50-arm-none-linux-gnueabi-i686-pc-linux-gnu.tar.bz2

                             

                            voodoo@voodoo-e6400:~$ export CROSS_COMPILE=/home/voodoo/arm-2010.09/bin/arm-none-linux-gnueabi-

                            voodoo@voodoo-e6400:~$ ${CROSS_COMPILE}gcc -v

                            Using built-in specs.

                            COLLECT_GCC=/home/voodoo/arm-2010.09/bin/arm-none-linux-gnueabi-gcc

                            COLLECT_LTO_WRAPPER=/home/voodoo/arm-2010.09/bin/../libexec/gcc/arm-none-linux-gnueabi/4.5.1/lto-wrapper

                            Target: arm-none-linux-gnueabi

                            Configured with: /scratch/nathan/arm-lite/src/gcc-4.5-2010.09/configure --build=i686-pc-linux-gnu --host=i686-pc-linux-gnu --target=arm-none-linux-gnueabi --enable-threads --disable-libmudflap --disable-libssp --disable-libstdcxx-pch --enable-extra-sgxxlite-multilibs --with-arch=armv5te --with-gnu-as --with-gnu-ld --with-specs='%{save-temps: -fverbose-asm} %{funwind-tables|fno-unwind-tables|mabi=*|ffreestanding|nostdlib:;:-funwind-tables} -D__CS_SOURCERYGXX_MAJ__=2010 -D__CS_SOURCERYGXX_MIN__=9 -D__CS_SOURCERYGXX_REV__=50 %{O2:%{!fno-remove-local-statics: -fremove-local-statics}} %{O*:%{O|O0|O1|O2|Os:;:%{!fno-remove-local-statics: -fremove-local-statics}}}' --enable-languages=c,c++ --enable-shared --enable-lto --enable-symvers=gnu --enable-__cxa_atexit --with-pkgversion='Sourcery G++ Lite 2010.09-50' --with-bugurl=https://support.codesourcery.com/GNUToolchain/ --disable-nls --prefix=/opt/codesourcery --with-sysroot=/opt/codesourcery/arm-none-linux-gnueabi/libc --with-build-sysroot=/scratch/nathan/arm-lite/install/arm-none-linux-gnueabi/libc --with-gmp=/scratch/nathan/arm-lite/obj/host-libs-2010.09-50-arm-none-linux-gnueabi-i686-pc-linux-gnu/usr --with-mpfr=/scratch/nathan/arm-lite/obj/host-libs-2010.09-50-arm-none-linux-gnueabi-i686-pc-linux-gnu/usr --with-mpc=/scratch/nathan/arm-lite/obj/host-libs-2010.09-50-arm-none-linux-gnueabi-i686-pc-linux-gnu/usr --with-ppl=/scratch/nathan/arm-lite/obj/host-libs-2010.09-50-arm-none-linux-gnueabi-i686-pc-linux-gnu/usr --with-host-libstdcxx='-static-libgcc -Wl,-Bstatic,-lstdc++,-Bdynamic -lm' --with-cloog=/scratch/nathan/arm-lite/obj/host-libs-2010.09-50-arm-none-linux-gnueabi-i686-pc-linux-gnu/usr --with-libelf=/scratch/nathan/arm-lite/obj/host-libs-2010.09-50-arm-none-linux-gnueabi-i686-pc-linux-gnu/usr --disable-libgomp --enable-poison-system-directories --with-build-time-tools=/scratch/nathan/arm-lite/install/arm-none-linux-gnueabi/bin --with-build-time-tools=/scratch/nathan/arm-lite/install/arm-none-linux-gnueabi/bin

                            Thread model: posix

                            gcc version 4.5.1 (Sourcery G++ Lite 2010.09-50)

                             

                            Regards,

                              • Robert Nelson designspecialist-micro 70 posts since
                                Mar 21, 2011
                                Currently Being Moderated
                                May 1, 2011 9:44 AM (in response to Timothy)
                                Re: Please help...

                                Hi Timothy,

                                tar (child): Downloads/arm-2010.09-50-arm-none-linux-gnueabi-i686-pc-linux-gnu.tar.bz2: Cannot open: No such file or directory

                                 

                                Based on that error message, double check where you actually downloaded it to..

                                 

                                Regards,

                                  • Robert Nelson designspecialist-micro 70 posts since
                                    Mar 21, 2011
                                    Currently Being Moderated
                                    May 1, 2011 6:32 PM (in response to Timothy)
                                    Re: Please help...

                                    Well, you have your ARM Cross Compiler now, so I would build the project...

                                     

                                    Where you using a Makefile (easist) or calling gcc directly?

                                     

                                    Modify your Makefile like shown:

                                     

                                    AS              = $(CROSS_COMPILE)as

                                    LD              = $(CROSS_COMPILE)ld

                                    CC              = $(CROSS_COMPILE)gcc

                                    CPP             = $(CC) -E

                                    AR              = $(CROSS_COMPILE)ar

                                    NM              = $(CROSS_COMPILE)nm

                                    STRIP           = $(CROSS_COMPILE)strip

                                    OBJCOPY         = $(CROSS_COMPILE)objcopy

                                    OBJDUMP         = $(CROSS_COMPILE)objdump

                                     

                                    Regards,

                                      • Robert Nelson designspecialist-micro 70 posts since
                                        Mar 21, 2011
                                        Currently Being Moderated
                                        May 2, 2011 12:11 PM (in response to Timothy)
                                        Re: Please help...

                                        Hi Timothy,

                                         

                                        A Makefile, is a simple file to control compilation of a program.  It's as old as unix..

                                        http://en.wikipedia.org/wiki/Make_(software)

                                         

                                        Since you don't know what a makefile is, then how did you compile your C program previously in ubuntu?  Did you call gcc directly or what?

                                         

                                        Regards,

                                          • Robert Nelson designspecialist-micro 70 posts since
                                            Mar 21, 2011
                                            Currently Being Moderated
                                            May 2, 2011 12:30 PM (in response to Timothy)
                                            Re: Please help...

                                            Hi Timothy,

                                             

                                            Then add the ${CROSS_COMPILE} prefix and run gcc.

                                             

                                            voodoo@work-p4:~$ ${CROSS_COMPILE}gcc -o test test.c

                                             

                                            Test with file:

                                             

                                            voodoo@work-p4:~$ file test

                                            test: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.16, not stripped

                                             

                                            ^^^ That file is now built for arm... ^^^

                                             

                                            As a makefile:

                                             

                                               CC     = ${CROSS_COMPILE}gcc
                                               CFLAGS = -g

                                               all: test

                                               test: test.o
                                                        # Commands start with TAB not spaces
                                                        $(CC) $(LDFLAGS) -o $@ $^

                                               test.o: test.c
                                                        $(CC) $(CFLAGS) -c -o $@ $<

                                               clean:
                                                        rm -f test test.o

                                               # This is GNU makefile extension to notify that roughly means: 'clean' does
                                               # not depend on any files in order to call it.
                                               .PHONY: clean
                                              • Robert Nelson designspecialist-micro 70 posts since
                                                Mar 21, 2011
                                                Currently Being Moderated
                                                May 2, 2011 1:34 PM (in response to Timothy)
                                                Re: Please help...

                                                Hi Timothy,

                                                timothy@ubuntu:~/Desktop$ ${CROSS_COMPILE}gcc -o sums sums.c -lm

                                                timothy@ubuntu:~/Desktop$ file sums

                                                sums: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, not stripped

                                                 

                                                Make sure "CROSS_COMPILE" is actually set.. "export CROSS_COMPILE=/path/to/arm/gcc" echo ${CROSS_COMPILE} will show the current path..

                                                 

                                                As a makefile:

                                                 

                                                   CC     = ${CROSS_COMPILE}gcc
                                                   CFLAGS = -g

                                                       .

                                                       .

                                                       .

                                                Do we type this at the terminal, after file sums command???

                                                 

                                                With the makefile, use "make all" to build everything, and "make clean" to cleanup files.

                                                 

                                                Regards,

                                                  • Robert Nelson designspecialist-micro 70 posts since
                                                    Mar 21, 2011
                                                    Currently Being Moderated
                                                    May 2, 2011 1:51 PM (in response to Timothy)
                                                    Re: Please help...

                                                    Hi Timothy,

                                                    timothy@ubuntu:~/Desktop$ export CROSS_COMPILE=/path/to/arm/gcc

                                                     

                                                    Replace "/path/to/arm/gcc" with the path to your gcc based arm cross compiler...

                                                     

                                                    For example on my system..

                                                     

                                                    export CROSS_COMPILE=/home/voodoo/arm-2010.09/bin/arm-none-linux-gnueabi-

                                                     

                                                    Regards,

                                                      • Robert Nelson designspecialist-micro 70 posts since
                                                        Mar 21, 2011
                                                        Currently Being Moderated
                                                        May 2, 2011 2:46 PM (in response to Timothy)
                                                        Re: Please help...

                                                        Hi Timothy,

                                                         

                                                        I'm guessing you've now either run the Makefile or called GCC directly to build your program for arm? Correct?

                                                         

                                                        Verify it's built for "arm" with "file <program>"

                                                         

                                                        I'd copy it to your arm target and run the program.

                                                         

                                                        Regards,

                                                          • Robert Nelson designspecialist-micro 70 posts since
                                                            Mar 21, 2011
                                                            Currently Being Moderated
                                                            May 2, 2011 3:08 PM (in response to Timothy)
                                                            Re: Please help...

                                                            Hi Timothy,

                                                             

                                                            Well, do you still have that OMAP L138 platform around from your previous posts and is it still working?

                                                             

                                                            I'd power the L138 back up and copy the "sums" arm binary to it that you just built and try to run the program..

                                                             

                                                            Regards,

                                                              • Robert Nelson designspecialist-micro 70 posts since
                                                                Mar 21, 2011
                                                                Currently Being Moderated
                                                                May 2, 2011 3:25 PM (in response to Timothy)
                                                                Re: Please help...

                                                                Hi Timothy,

                                                                 

                                                                Can you please be more specific about your question.

                                                                 

                                                                What in the Makefile are you trying to modify? (it's an actual file called "Makefile" in the same directory as your *.c files)  You can open it in any editor, just make sure that command lines start with "TAB"'s not "spaces"..

                                                                 

                                                                Otherwise with atleast CROSS_COMPILE defined it should work exactly the same way as when you called gcc from the command line..

                                                                 

                                                                CROSS_COMPILE = /path/to/your/arm/cross/arm-none-linux-gnueabi-

                                                                CC     = ${CROSS_COMPILE}gcc

                                                                 

                                                                Regards,

                                                                  • Robert Nelson designspecialist-micro 70 posts since
                                                                    Mar 21, 2011
                                                                    Currently Being Moderated
                                                                    May 2, 2011 3:57 PM (in response to Timothy)
                                                                    Re: Please help...

                                                                    Hi Timothy,

                                                                     

                                                                    Well you can use any editor from the terminal, nano/vim/gedit/kate/etc...

                                                                     

                                                                    But in this case i'll use nano:

                                                                     

                                                                    nano Makefile

                                                                     

                                                                    CROSS_COMPILE = arm-linux-gnueabi-
                                                                    CC = ${CROSS_COMPILE}gcc
                                                                    CFLAGS = -g
                                                                    all: test
                                                                    
                                                                    test: test.o
                                                                        # Commands start with TAB not spaces
                                                                        $(CC) $(LDFLAGS) -o $@ $^
                                                                    
                                                                    test.o: test.c
                                                                        # Commands start with TAB not spaces
                                                                        $(CC) $(CFLAGS) -c -o $@ $<
                                                                    
                                                                    clean:
                                                                        # Commands start with TAB not spaces
                                                                        rm -f test test.o
                                                                    
                                                                    .PHONY: clean 
                                                                    

                                                                     

                                                                    Note: "TABS" don't work in this forum interface, so everything that's a command needs to be one "TAB" and use your own CROSS_COMPILER define, that one is for my install...

                                                                     

                                                                    Hit "ctrl-x" -> "y" -> "Enter" to save and exit..

                                                                     

                                                                    "make all" will build test.c

                                                                    "make clean" will cleanup everything..

                                                                     

                                                                    Regards,

                                                                      • Robert Nelson designspecialist-micro 70 posts since
                                                                        Mar 21, 2011
                                                                        Currently Being Moderated
                                                                        May 2, 2011 5:02 PM (in response to Timothy)
                                                                        Re: Please help...

                                                                        Hi Timothy,

                                                                         

                                                                        CC      = ${CROSS_COMPILE}gcc -o sums sums.c -lm

                                                                         

                                                                        That's definitely not a good place to add "-o sums sums.c -lm"

                                                                         

                                                                        First add "-lm" too:

                                                                        CC      = ${CROSS_COMPILE}gcc

                                                                        CFLAGS = -g

                                                                        LDFLAGS = -lm

                                                                        the "-o sums sums.c" is already taken care of by:

                                                                        sums: sums.o

                                                                                $(CC) $(LDFLAGS) -o $@ $^

                                                                         

                                                                        Make sure sums.c actually exists...

                                                                        timothy@ubuntu:~$ make all

                                                                        make: *** No rule to make target `sums.c', needed by `sums.o'.  Stop.

                                                                        voodoo@work-p4:~/sums$ ls

                                                                        Makefile  sums.c

                                                                        voodoo@work-p4:~/sums$ make all

                                                                        arm-linux-gnueabi-gcc -g -c -o sums.o sums.c

                                                                        arm-linux-gnueabi-gcc -lm -o sums sums.o

                                                                        voodoo@work-p4:~/sums$ ls -lh

                                                                        total 20K

                                                                        -rw-r--r-- 1 voodoo voodoo  232 2011-05-02 16:56 Makefile

                                                                        -rwxr-xr-x 1 voodoo voodoo 6.0K 2011-05-02 17:02 sums

                                                                        -rw-r--r-- 1 voodoo voodoo   80 2011-05-02 16:53 sums.c

                                                                        -rw-r--r-- 1 voodoo voodoo 2.8K 2011-05-02 17:02 sums.o

                                                                        voodoo@work-p4:~/sums$ file sums

                                                                        sums: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.16, not stripped

                                                                         

                                                                        Regards,

                                                                          • Robert Nelson designspecialist-micro 70 posts since
                                                                            Mar 21, 2011
                                                                            Currently Being Moderated
                                                                            May 2, 2011 7:10 PM (in response to Timothy)
                                                                            Re: Please help...

                                                                            Hi Timothy,

                                                                             

                                                                            In one of your first messages you said:

                                                                             

                                                                            "The ARM processor that I am targetting is the ARM processor from the OMAP L138 platform. ( It recides on the Hawkboard -  a microcomputer)"

                                                                             

                                                                            I'm assuming you actually have this device?

                                                                             

                                                                            I'm also been assuming it's running an OS based on linux as you never answered this question either. "Are we talking bare-metal or os (linux/qnx) based system?"

                                                                             

                                                                            So right now you have an ARM binary "sums" that is ready to be run on your platform (OMAP L138 running Linux).  So either copy the "sums" binary to the sd/mmc card, or over a usb flash drive, or over a network, etc.. (there's 1000's of ways..)  Once you do that, you should be able to run your "sums" binary right on the OMAP L138..

                                                                             

                                                                            Regards,

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • Correct Answers - 4 points
  • Helpful Answers - 3 points