CrossCompile_on_Debian - h-sendai/RaspberryPi GitHub Wiki

準備

armhf用バイナリを生成するコンパイラをセットする:

sudo apt-get install gcc-arm-linux-gnueabihf

これでstdioなどを使う単純なプログラムはarm-linux-gnueabihf-gccを使えば作れるようになる。

例題

# makefile
CC = arm-linux-gnueabihf-gcc
PROG = sample
CFLAGS += -g -O2 -Wall

all: $(PROG)

sample.c:

#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("hello, world\n");

    return 0;
}

でmakeしたプログラムsampleをraspberry piにコピーして実行するとちゃんと表示される。Raspberry Pi上でlddでみると

% ldd sample
linux-vdso.so.1 (0x7ee92000)
/usr/lib/arm-linux-gnueabihf/libarmmem.so (0x76f19000)
libc.so.6 => /lib/arm-linux-gnueabihf/libc.so.6 (0x76dc6000)
/lib/ld-linux-armhf.so.3 (0x76f41000)

Mutiarchのセット

各種ライブラリのセットを便利にするために、 https://wiki.debian.org/Multiarch/HOWTO に従ってMultiarchの設定をする。Raspbianはarmhf (hf: hard float)。

sudo dpkg --add-architecture armhf
sudo apt-get update
まだやっていなければ sudo apt-get install gcc-arm-linux-gnueabihf

例題: less を configure; makeする

libtinfoライブラリが必要なのでapt-get install libtinfo-dev:armhfでセットする:

debian% sudo apt-get install libtinfo-dev:armhf
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  gcc-6-base:armhf libc6:armhf libgcc1:armhf libtinfo5:armhf
Suggested packages:
  glibc-doc:armhf locales:armhf
The following NEW packages will be installed:
  gcc-6-base:armhf libc6:armhf libgcc1:armhf libtinfo-dev:armhf
  libtinfo5:armhf
0 upgraded, 5 newly installed, 0 to remove and 0 not upgraded.
Need to get 2,915 kB of archives.
After this operation, 9,603 kB of additional disk space will be used.
Do you want to continue? [Y/n] 
(以下略)

configure時に --host=arm-linux-gnueabihf オプションを付けることで クロスコンパイルであり、コンパイルドライバとして arm-linux-gnueabihf-gccを使う設定のMakefileができる。

tar xf less-487.tar.gz
cd less-487
./configure --host=arm-linux-gnueabihf
make

これで正常にコンパイルでき、raspberry piにコピーして動作することを確認した。

例題: Emacsをconfigure; makeする

もう少し大きなプログラムを試すということでEmacsを--without-xでconfigure してmakeしてみた。途中でコンパイルがとまる。

  GEN      lisp.mk
  GEN      globals.h
/bin/bash: ../lib-src/make-docfile: cannot execute binary file: Exec format error
Makefile:579: recipe for target 'gl-stamp' failed
make[1]: *** [gl-stamp] Error 126
make[1]: Leaving directory '/home/sendai/src/emacs-25.3/src'
Makefile:398: recipe for target 'src' failed
make: *** [src] Error 2

makeの途中でできる lib-src/make-docfile をmake中に実行するように なっている。 lib-src/make-docfileは

lib-src/make-docfile: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV),
dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 3.2.0,
BuildID[sha1]=e370a3805758caec9a66eefc35c979c80549263f, not stripped

という具合にEmacsのmake中にarmバイナリとして生成されたものでx86_64上では 動作しないものになってしまっている。

同じような報告が https://lists.gnu.org/archive/html/bug-gnu-emacs/2013-01/msg00676.html にある。

⚠️ **GitHub.com Fallback** ⚠️