Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross-Compiling for RaspBerry Pi

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

When copy the helloWorld binary to raspBerry by TFTP and give it execution permissions with chmod, the next error appears:

"Illegal instruction"

If 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:

/*
 * main.cpp
 *
 *  Created on: 26/06/2012
 *      Author: ccortiz
 */

#include <iostream>
using namespace std;

int main(void){

    cout << "Hello Cross Compilling for ARM!" << endl << flush;
    return 0;
}

How could I compile and run my program in a right way? Thanks.

like image 456
Cesar Ortiz Avatar asked Jun 26 '12 19:06

Cesar Ortiz


3 Answers

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).

like image 67
Mikhail Kupchik Avatar answered Oct 21 '22 12:10

Mikhail Kupchik


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

like image 3
Cesar Ortiz Avatar answered Oct 21 '22 11:10

Cesar Ortiz


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

like image 3
hithwen Avatar answered Oct 21 '22 11:10

hithwen