13

With a Raspberry Pi and from my computer, I'm trying to cross-compile a simple helloWorld written in C++. I'm using the Code Sourcery toolchain for Linux to compile.

When copying the helloWorld binary to the Pi by TFTP and giving it execution permissions with chmod, the next error appears:

"Illegal instruction"

If I make a 'file' over binary I get: raspberry: ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, stripped

This is because I used -static -static-libstdc++ when linking.

If I don't use static linking, the error is: "Segmentation fault"

The Code:

#include <iostream>
using namespace std;

int main(void){
    cout << "Hello Cross Compiling for ARM!" << endl << flush;
    return 0;
}

How could I compile and run my program in the right way?

Cristik
  • 30,989
  • 25
  • 91
  • 127
Cesar Ortiz
  • 884
  • 1
  • 8
  • 25
  • please post hello world source and a disassembly if your binary. – old_timer Jun 26 '12 at 19:43
  • I thought one of the benefits of RaspberryPi is that it runs a full OS, you don't need to cross-compile. – Mark Ransom Jun 26 '12 at 19:52
  • 3
    Yes, but compiling on a Raspberry is slower than compiling in my Core i5. But also I want to program with my beloved Eclipse. – Cesar Ortiz Jun 26 '12 at 19:56
  • @MarkRansom that might require building the whole toolchain on the target though, which could be non-trivial. – juanchopanza Jun 26 '12 at 19:56
  • You could have a look at [scratchbox2](http://russelldavis.org/2011/09/07/setting-up-scratchbox2-from-scratch-for-the-raspberry-pi/), which allows you to use the rootfs of the target on your host. Although I can see it would be good to understand why your simple hello world doesn't work. It was certainly trivial on the beagleboard. – juanchopanza Jun 26 '12 at 19:59
  • 2
    if build the whole toolchain on the target system is a requirement, What is the sense of download/buy a commercial toolchain? – Cesar Ortiz Jun 26 '12 at 20:00
  • 3
    I obtained "Segmentation fault" because transmiting my binary by TFTP to raspberry, I used ASCII mode instead Binary mode. Beginer's things. – Cesar Ortiz Jul 04 '12 at 15:10
  • See also http://stackoverflow.com/questions/19162072/installing-raspberry-pi-cross-compiler – Ross Rogers Feb 11 '14 at 18:55
  • 1
    @CesarOrtiz I know it's so much later, but I just can't help myself! `endl` already `flush`es, so you deliberately flush twice. This is why I prefer using `'\n'` to `endl` in the first place. – BoBTFish Feb 05 '15 at 21:52

6 Answers6

8

The reason why are you getting Segmentation fault error is different ABI. Raspberry Pi when running Raspbian is using linux-arm-gnueabihf ABI which assumes hardfp and VFP support in hardware (which is rare in ARMv6 environment) so requires some additional patches for GCC and EGLIBC (these patches can be found in Raspbian repository).

Your Code Sourcery cross-toolchain most likely does not have these patches, so it's using another ABI (linux-arm-gnueabi) hence the crash at runtime (static linking works because kernel ABI does not depend on hardfp/softfp).

Another possible reason why you may be getting Illegal Instruction error is Code Sourcery cross-toolchain configured for ARMv7 and Raspberry Pi is ARMv6. But in this case both static and dynamic linking will yield the same error.

Here is a step-by-step guide how to build Raspberry Pi cross compiler in Windows, both hardfp/softfp ABI versions. Resulting cross-compiler supports C++ and does not depend on cygwin runtime library (cygwin1.dll).

Mikhail Kupchik
  • 308
  • 2
  • 8
3

The problem was to use ASCII mode instead of binary mode in my FTP transfers. Today I prefer to use SFTP (SSH). Thanks.

Cesar Ortiz
  • 884
  • 1
  • 8
  • 25
3

I'd recommend trying biicode, it automatically sets up the cross compiler environment for you and sends generated binaries to the raspberry after building

hithwen
  • 2,154
  • 28
  • 46
1

You could try the toolchain at: https://github.com/kallaballa/Raspberry-GCC-4.7.3

It's a pre-built gcc-4.7.3 toolchain for armv6 with hardfp using gnueabi. I'm using it to cross compile c++11 for a raspberrian target.

Please note it only works on linux x86_64 hosts.

kallaballa
  • 337
  • 2
  • 8
1

My recommendation is here, http://hertaville.com/2012/09/28/development-environment-raspberry-pi-cross-compiler/ He does a good job on explaining how to use Eclipse to compile HelloWorld.cpp, download, and execute it in Raspberry pi board. Check it out.

Hatto
  • 65
  • 2
0

Using Visual studio and Visual GDB plugin you can download cross compiler for raspberry pi. After that you can create a ssh connection and also you can deploy your program remotely into raspberry pi.

Arun Chand
  • 384
  • 4
  • 18