aaa801
Posts: 428
Joined: Mon Jun 04, 2012 9:06 pm
Location: Berkshire

Cross compiling?

Mon Jun 04, 2012 9:09 pm

How would i go about setting up a cross compiling toolchain for the pi?
giving a host of 64bit ubuntu or debian

User avatar
bhoga
Posts: 10
Joined: Sun Jun 03, 2012 7:33 pm
Location: Brighton, UK

Re: Cross compiling?

Mon Jun 04, 2012 9:51 pm

Take a look here:

http://elinux.org/RPi_Kernel_Compilation#Ubuntu_Linux

I have 64-bit 12.04 Ubuntu, and it worked as per the instructions.

cupcake
Posts: 59
Joined: Sat Jan 21, 2012 9:00 pm

Re: Cross compiling?

Tue Jun 05, 2012 8:12 pm

Forgive my ignorance, but are those the steps to setup cross-compilation from a different platform to the ARM of the RPi?

If so, following the last step, does one simply compile with gcc and the output is then ARM-based?

User avatar
jecxjo
Posts: 158
Joined: Sat May 19, 2012 5:22 pm
Location: Minneapolis, MN (USA)

Re: Cross compiling?

Wed Jun 06, 2012 6:52 pm

It gets you a few steps closer. Once you follow the steps to install the cross compiler for your desktop's distro you should have a compiler along the lines of: /usr/bin/arm-unknown-linux-gnueabi-gcc or /opt/local/bin/arm-unknown-linux-gnueabi-gcc

So to make this simpler we'll just make a little hello world program.

Code: Select all

#include <stdio.h>

int main ()
{
  printf("Hello Cross Compiled Pi\n");
  return 0;
}
We can compile it natively via:

Code: Select all

# gcc -o native-hello hello.c
# file native-hello
native-hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, not stripped
# ./native-hello
Hello Cross Compiled Pi
#
The file command shows us that the file is built for a 64-bit x86-64 Linux system. If we want to cross compile this file we would change the compiler we call:

Code: Select all

# arm-unknown-linux-gnueabi-gcc -o arm-hello hello.c
# file arm-hello
arm-hello: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.27, not stripped
#
Now if you copy that file onto your Pi you can run it and get the printout we saw in the native build. If you are looking to build an entire project you'll want to look into how to run their build scripts using cross compilers.
xmpp: [email protected]
Blog: http://jecxjo.motd.org/code

User avatar
johnbeetem
Posts: 945
Joined: Mon Oct 17, 2011 11:18 pm
Location: The Mountains

Re: Cross compiling?

Wed Jun 06, 2012 7:54 pm

You can also get the CodeSourcery G++ Lite port of GCC: here's a wiki page, targeted primarily at BeagleBoard: http://www.elinux.org/ARMCompilers. I've successfully used version "2011.03-41 for ARM GNU/Linux" on 32-bit Ubuntu 11.10.

User avatar
RichardUK
Posts: 255
Joined: Fri Jun 01, 2012 5:12 pm

Re: Cross compiling?

Thu Jun 07, 2012 10:25 am

When you install this tool chain does it also get all the standard libs for the system too?
Am I correct in thinking for doing RPi specifics like GLES all I have to do then is copy the folder from /etc/vc to my build PC and include that in the normal way in my make files?

irknowles
Posts: 2
Joined: Sat Jun 02, 2012 6:56 am

Re: Cross compiling?

Thu Jun 07, 2012 8:25 pm

Hello - I am following the work flows to cross compile the raspberry kernel. However, I have not been able to get any arm executable to run on the pi.

My goal is to Cross Compile the full Linux Kernel adding new modules using make menuconfig

The basics of getting my tool chain right.
Some details (hoping this might help other debug the same issues) :

Host Machine Running Ubuntu 12.04 LTE with all latest updates.
Linux 3.2.0-24-generic-pae #39-Ubuntu SMP Mon May 21 18:54:21 UTC 2012 i686 athlon i386 GNU/Linux

Pi
Linux raspberrypi 3.1.9+ #90 Wed Apr 18 18:23:05 BST 2012 armv6l GNU/Linux

Toolchain - as described in http://elinux.org/RPi_Kernel_Compilation
- sudo apt-get install gcc-4.6-arm-linux-gnueabi

I can compile all OK a simple Hello World program (as shown on this page) using the standard gcc on Ubuntu 12.04. This runs all OK and outputs the right stuff.

I then use the cross compile to compile for Pi. No errors.
I copy the output to the platform and run the executable and then get a Segmentation Fault.

Looking at the file using >file ./a.out
./a.out: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.31, not stripped

Can anyone see anything wrong ?

Note - I also do File /bin/ls and see :-
/bin/ls: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped

Could the issue be the version of Linux that is build for is higher in version

Look forward to any advise out there...

Ian

mjbright
Posts: 9
Joined: Mon Jun 25, 2012 7:38 am

Re: Cross compiling?

Thu Jun 28, 2012 5:48 pm

I have this problem also.
I'm running on a Ubuntu 12.04 and I've followed the instructions on the building a kernel page,
but when I run a cross-compiled Hello World I get a Segmentation Fault.

I'm compiling under Ubuntu 12.04 as:
/usr/bin/arm-linux-gnueabi-gcc -o arm3.out HelloWorld.c

Running on the Pi gives:
$ ./arm.out
Segmentation fault

Running the program natively works fine.

Below is the output of strace on the cross-compiled executeable:
Any clue?

execve("./arm.out", ["./arm.out"], [/* 17 vars */]) = 0
brk(0) = 0x8f6000
uname({sys="Linux", node="raspberrypi", ...}) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x400c8000
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=41200, ...}) = 0
mmap2(NULL, 41200, PROT_READ, MAP_PRIVATE, 3, 0) = 0x4013a000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/lib/arm-linux-gnueabi/libc.so.6", O_RDONLY) = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0(\0\1\0\0\0XX\1\0004\0\0\0"..., 512) = 512
lseek(3, 1211164, SEEK_SET) = 1211164
read(3, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 1400) = 1400
lseek(3, 1210724, SEEK_SET) = 1210724
read(3, "A'\0\0\0aeabi\0\1\35\0\0\0\0054T\0\6\2\10\1\t\1\22\4\24\1\25\1"..., 40) = 40
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x4018b000
fstat64(3, {st_mode=S_IFREG|0755, st_size=1212564, ...}) = 0
mmap2(NULL, 1254688, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x40287000
mprotect(0x403ad000, 28672, PROT_NONE) = 0
mmap2(0x403b4000, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x125) = 0x403b4000
mmap2(0x403b7000, 9504, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x403b7000
close(3) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x400ef000
set_tls(0x400ef4c0, 0x400efb97, 0x400efb98, 0x400ef4c0, 0x400d0000) = 0
mprotect(0x403b4000, 8192, PROT_READ) = 0
mprotect(0x10000, 4096, PROT_READ) = 0
mprotect(0x400cf000, 4096, PROT_READ) = 0
munmap(0x4013a000, 41200) = 0
--- SIGSEGV (Segmentation fault) @ 0 (0) ---
+++ killed by SIGSEGV +++

mjbright
Posts: 9
Joined: Mon Jun 25, 2012 7:38 am

Re: Cross compiling?

Thu Jun 28, 2012 5:50 pm

... and file arm.out gives:

arm.out: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.31, BuildID[sha1]=0x7f7e5a58f994614441eb2aa813f571a09c1d1b82, not stripped

Max

Re: Cross compiling?

Thu Jun 28, 2012 11:07 pm

mjbright wrote:I'm compiling under Ubuntu 12.04 as:
/usr/bin/arm-linux-gnueabi-gcc -o arm3.out HelloWorld.c
You might need more compiler flags to produce executables compatible with the Pi, probably something like: -mcpu=arm1176jzf-s -mfloat-abi=soft

Easier to grab the arm-bcm2708-linux-gnueabi-gcc from github instead, which has the right flags by default: https://github.com/raspberrypi/tools

mjbright
Posts: 9
Joined: Mon Jun 25, 2012 7:38 am

Re: Cross compiling?

Fri Jun 29, 2012 10:45 am

Thanks a lot, I'm now using the arm-bcm2708-linux-gnueabi tools from github and that did the trick. :D

That said, I couldn't figure out how to use them, so searching around, I adapted what was written in this post:
http://blog.greg-net.co.uk/post/2122805 ... spberry-pi


So to build my hello world, I'm now using the following.
I'll see if I can put this in the Wiki.

export TOOL_PREFIX=$HOME/src/git/raspberrypi/tools/arm-bcm2708/linux-x86/bin/arm-bcm2708-linux-gnueabi
export CXX=$TOOL_PREFIX-g++
export AR=$TOOL_PREFIX-ar
export RANLIB=$TOOL_PREFIX-ranlib
export CC=$TOOL_PREFIX-gcc
export LD=$TOOL_PREFIX-ld
export CCFLAGS="-march=armv4 -mno-thumb-interwork"

$CC -o arm.out HelloWorld.c

Max

Re: Cross compiling?

Fri Jun 29, 2012 1:35 pm

mjbright wrote: That said, I couldn't figure out how to use them
The same way you were using gcc.

/path-where-you-put-the-stuff/arm-bcm2708/linux-x86/bin/arm-bcm2708-linux-gnueabi-gcc -o executable HelloWorld.c

export TOOL_PREFIX=$HOME/src/git/raspberrypi/tools/arm-bcm2708/linux-x86/bin/arm-bcm2708-linux-gnueabi
export CXX=$TOOL_PREFIX-g++
export AR=$TOOL_PREFIX-ar
export RANLIB=$TOOL_PREFIX-ranlib
export CC=$TOOL_PREFIX-gcc
export LD=$TOOL_PREFIX-ld
export CCFLAGS="-march=armv4 -mno-thumb-interwork"
Setting variables like that can be useful if you want to compile existing projects using Makefiles, but is not necessary for single files.

For simple cross compiling instructions, take a look at the manual Synology put together for people wanting to mod their NAS devices: http://download.synology.com/download/d ... _Guide.pdf
(just replace the "arm-marvell-linux-gnu" they do there with arm-bcm2708-linux-gnueabi)

User avatar
hithwen
Posts: 1
Joined: Tue Jan 07, 2014 1:22 pm

Re: Cross compiling?

Mon Jan 20, 2014 2:59 pm

Hi,
For those who are still struggling to set up the cross compiler and/or executing generated binaries I'd suggest trying out biicode, its a tool that will donwnload and set up the cross-compilers for you also will send generated binaries directly to the raspberry pi :D (Only works for ubuntu though)

twobob
Posts: 6
Joined: Mon Jun 02, 2014 2:30 pm

Re: Cross compiling?

Sun Jun 08, 2014 10:01 pm

biicode link should be http://docs.biicode.com/raspberrypi.html

(FWIW)

jhillix
Posts: 8
Joined: Sun Jul 06, 2014 4:13 pm

Re: Cross compiling?

Sun Jul 06, 2014 6:45 pm

Building your own cross-compiler from the ground up is not that difficult. What makes the process a burden is finding the right recipe, i.e. gcc, glibc, etc compatibilities. Also, make sure your machine has plenty of memory and processing power. You'd be surprised at how often the process breaks because of insufficient memory, 1 or 2 GB is not really enough.

The best book that I have read to get me going is Pro Linux Embedded Systems. The book can be hard to follow at times but is still a good resource.
Last edited by jhillix on Sun Jul 06, 2014 8:51 pm, edited 1 time in total.

User avatar
ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 6460
Joined: Fri Jul 29, 2011 5:36 pm

Re: Cross compiling?

Sun Jul 06, 2014 7:53 pm

My favourite cross-dev environment is this one. https://github.com/bmanojlovic/rpi-cross-compile
I've tried everything and that's the only one that just works without wasting your time.

jhillix
Posts: 8
Joined: Sun Jul 06, 2014 4:13 pm

Re: Cross compiling?

Thu Jul 10, 2014 3:03 am

I have started to put together a wiki with my How To documents. It's still in the infancy stage and might have some copy & paste errors 8D But feel free to have a look:

https://how-to-build-for-arm.wikispaces.com/

adibacco
Posts: 1
Joined: Fri Aug 07, 2015 8:56 pm

Re: Cross compiling?

Fri Aug 07, 2015 9:03 pm

I got the same problem with a simple "hello world" program. I built the program on Ubuntu x86 and transferred to the PI2 target using Filezilla ftp client.
After some minutes spent panicking ... eventually I checked the md5 of local executable and md5 on PI2 and ... bingo, they were different.
That stupid ftp client was "auto" choosing the transfer mode (ascii or binary), for some reason it was using ASCII to transfer my executable.

inforide
Posts: 2
Joined: Wed Dec 09, 2015 11:51 am

Re: Cross compiling?

Wed Dec 09, 2015 11:59 am

There is my step-by-step guide and some theoretical backgrounds for cross compiler: http://rpilinuxdev.blogspot.ro/
I used this to compile quite complex stuff like GStreamer 1.4 and few media apps on RPI B+.

the-alchemist
Posts: 4
Joined: Wed Aug 03, 2016 4:27 am

Re: Cross compiling?

Mon Oct 24, 2016 6:48 pm

inforide wrote:There is my step-by-step guide and some theoretical backgrounds for cross compiler: http://rpilinuxdev.blogspot.ro/
Unfortunately, that blog is invite only :(

inforide
Posts: 2
Joined: Wed Dec 09, 2015 11:51 am

Re: Cross compiling?

Thu Jan 12, 2017 3:32 pm

I have activated the blog.
Alternatively you might take a look here : https://github.com/ionutneicu/rpi-cross-scripts

mleiva
Posts: 3
Joined: Sun Apr 09, 2017 11:18 pm

Re: Cross compiling?

Sat Feb 24, 2018 2:55 am

You can use this SDK to cross compile kernel, libraries and create a bootable SD image.

https://bitbucket.org/manuelleiva/rpisys

Basically to create a complete SD image you have to download the tool and run:

Code: Select all

# Configure board
./configure --board raspberry_pi_3/raspberry_pi_3.defs
# Build system
make
Create a bootable image
# Create SD image
make image-sd

artok
Posts: 1
Joined: Sun Jun 10, 2018 11:07 am

Re: Cross compiling?

Sun Jun 10, 2018 4:45 pm

Revisiting old topic. After doing some research around the net, I decided to do platform which allows to build RPI binaries basically on any host OS. (Linux, macOS, Windows) Using Vagrant and VirtualBox. I use this to make RPI binaries without "polluting" the RPI with -dev libraries etc.
Someone might find this helpful:

https://github.com/djazo/rpi-builder

Return to “General programming discussion”