目次

emdebian のリポジトリを追加し、クロスコンパイラを導入する方法

armhfクロスコンパイル環境を作る - Qiita をやってみます。


設定

apt-line の追加

/etc/apt/sources.list.d/crosstools.list を追加します。

root@jessie:~# nano -w /etc/apt/sources.list.d/crosstools.list
deb http://emdebian.org/tools/debian/ jessie main


curl の導入

root@jessie:~# apt-get install curl
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  libcurl3
The following NEW packages will be installed:
  curl libcurl3
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 459 kB of archives.
After this operation, 933 kB of additional disk space will be used.
Do you want to continue? [Y/n] 
Get:1 http://ftp.jaist.ac.jp/debian/ jessie/main libcurl3 amd64 7.38.0-4+deb8u2 [259 kB]
Get:2 http://ftp.jaist.ac.jp/debian/ jessie/main curl amd64 7.38.0-4+deb8u2 [200 kB]
Fetched 459 kB in 3s (116 kB/s)
Selecting previously unselected package libcurl3:amd64.
(Reading database ... 31327 files and directories currently installed.)
Preparing to unpack .../libcurl3_7.38.0-4+deb8u2_amd64.deb ...
Unpacking libcurl3:amd64 (7.38.0-4+deb8u2) ...
Selecting previously unselected package curl.
Preparing to unpack .../curl_7.38.0-4+deb8u2_amd64.deb ...
Unpacking curl (7.38.0-4+deb8u2) ...
Processing triggers for man-db (2.7.0.2-5) ...
Setting up libcurl3:amd64 (7.38.0-4+deb8u2) ...
Setting up curl (7.38.0-4+deb8u2) ...
Processing triggers for libc-bin (2.19-18+deb8u1) ...


archive keyのインストール

root@jessie:~# curl http://emdebian.org/tools/debian/emdebian-toolchain-archive.key | apt-key add -
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2398  100  2398    0     0    394      0  0:00:06  0:00:06 --:--:--   524
OK


crossbuild-essential-armhfのインストール

root@jessie:~# dpkg --add-architecture armhf
root@jessie:~# apt-get update
Ign http://ftp.jaist.ac.jp jessie InRelease
Hit http://ftp.jaist.ac.jp jessie-updates InRelease                                                       
Hit http://ftp.jaist.ac.jp jessie Release.gpg                                                                
Hit http://ftp.jaist.ac.jp jessie Release                                                                    
Hit http://ftp.jaist.ac.jp jessie-updates/main Sources                                                      
Hit http://ftp.jaist.ac.jp jessie-updates/contrib Sources                                                 
Get:1 http://ftp.jaist.ac.jp jessie-updates/main amd64 Packages/DiffIndex [367 B]          
Hit http://ftp.jaist.ac.jp jessie-updates/contrib amd64 Packages             
Hit http://ftp.jaist.ac.jp jessie-updates/contrib Translation-en                            
Hit http://ftp.jaist.ac.jp jessie-updates/main Translation-en                
Hit http://ftp.jaist.ac.jp jessie/main Sources                                                    
Hit http://security.debian.org jessie/updates InRelease                                    
Hit http://ftp.jaist.ac.jp jessie/main amd64 Packages                                      
Hit http://ftp.jaist.ac.jp jessie/main Translation-en                                         
Hit http://security.debian.org jessie/updates/main Sources                                    
Hit http://security.debian.org jessie/updates/contrib Sources   
Hit http://security.debian.org jessie/updates/main amd64 Packages 
Get:2 http://emdebian.org jessie InRelease [30.7 kB]              
Hit http://security.debian.org jessie/updates/contrib amd64 Packages        
Hit http://security.debian.org jessie/updates/contrib Translation-en         
Hit http://security.debian.org jessie/updates/main Translation-en            
Get:3 http://emdebian.org jessie/main amd64 Packages [14.8 kB]             
Ign http://emdebian.org jessie/main Translation-en_US                     
Ign http://emdebian.org jessie/main Translation-en
Fetched 45.9 kB in 4s (9,391 B/s)
Reading package lists... Done
root@jessie:~# apt-cache search crossbuild
crossbuild-essential-arm64 - Informational list of cross-build-essential packages
crossbuild-essential-armel - Informational list of cross-build-essential packages
crossbuild-essential-armhf - Informational list of cross-build-essential packages
crossbuild-essential-mipsel - Informational list of cross-build-essential packages
crossbuild-essential-powerpc - Informational list of cross-build-essential packages
crossbuild-essential-ppc64el - Informational list of cross-build-essential packages
root@jessie:~# apt-get install crossbuild-essential-armhf
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  binutils binutils-arm-linux-gnueabihf build-essential cpp cpp-4.9 cpp-4.9-arm-linux-gnueabihf dpkg-cross
  dpkg-dev fakeroot g++ g++-4.9 g++-4.9-arm-linux-gnueabihf g++-arm-linux-gnueabihf gcc gcc-4.9
  gcc-4.9-arm-linux-gnueabihf gcc-4.9-base:armhf gcc-arm-linux-gnueabihf libalgorithm-diff-perl
  libalgorithm-diff-xs-perl libalgorithm-merge-perl libasan1 libasan1:armhf libatomic1 libatomic1:armhf
  libc-dev-bin libc6:armhf libc6-dev libc6-dev:armhf libcilkrts5 libcloog-isl4 libconfig-auto-perl
  libconfig-inifiles-perl libdebian-dpkgcross-perl libdpkg-perl libfakeroot libfile-fcntllock-perl
  libfile-homedir-perl libfile-which-perl libgcc-4.9-dev libgcc-4.9-dev:armhf libgcc1:armhf libgomp1
  libgomp1:armhf libisl10 libitm1 liblist-moreutils-perl liblsan0 libmpc3 libmpfr4 libquadmath0
  libstdc++-4.9-dev libstdc++-4.9-dev:armhf libstdc++6:armhf libtsan0 libubsan0 libubsan0:armhf
  libxml-simple-perl libyaml-libyaml-perl libyaml-perl linux-libc-dev linux-libc-dev:armhf make manpages-dev
Suggested packages:
  binutils-doc cpp-doc gcc-4.9-locales binutils-multiarch debian-keyring g++-multilib g++-4.9-multilib
  gcc-4.9-doc libstdc++6-4.9-dbg libstdc++6-4.9-dbg:armhf gcc-multilib autoconf automake libtool flex bison
  gdb gcc-doc gcc-4.9-multilib libgcc1-dbg libgomp1-dbg libitm1-dbg libatomic1-dbg libasan1-dbg liblsan0-dbg
  libtsan0-dbg libubsan0-dbg libcilkrts5-dbg libquadmath0-dbg libgcc1-dbg:armhf libgomp1-dbg:armhf
  libitm1-dbg:armhf libatomic1-dbg:armhf libasan1-dbg:armhf liblsan0-dbg:armhf libtsan0-dbg:armhf
  libubsan0-dbg:armhf libcilkrts5-dbg:armhf libquadmath-dbg:armhf glibc-doc:armhf locales:armhf glibc-doc
  manpages-dev:armhf libstdc++-4.9-doc libstdc++-4.9-doc:armhf libyaml-shell-perl make-doc
The following NEW packages will be installed:
  binutils binutils-arm-linux-gnueabihf build-essential cpp cpp-4.9 cpp-4.9-arm-linux-gnueabihf
  crossbuild-essential-armhf dpkg-cross dpkg-dev fakeroot g++ g++-4.9 g++-4.9-arm-linux-gnueabihf
  g++-arm-linux-gnueabihf gcc gcc-4.9 gcc-4.9-arm-linux-gnueabihf gcc-4.9-base:armhf gcc-arm-linux-gnueabihf
  libalgorithm-diff-perl libalgorithm-diff-xs-perl libalgorithm-merge-perl libasan1 libasan1:armhf
  libatomic1 libatomic1:armhf libc-dev-bin libc6:armhf libc6-dev libc6-dev:armhf libcilkrts5 libcloog-isl4
  libconfig-auto-perl libconfig-inifiles-perl libdebian-dpkgcross-perl libdpkg-perl libfakeroot
  libfile-fcntllock-perl libfile-homedir-perl libfile-which-perl libgcc-4.9-dev libgcc-4.9-dev:armhf
  libgcc1:armhf libgomp1 libgomp1:armhf libisl10 libitm1 liblist-moreutils-perl liblsan0 libmpc3 libmpfr4
  libquadmath0 libstdc++-4.9-dev libstdc++-4.9-dev:armhf libstdc++6:armhf libtsan0 libubsan0 libubsan0:armhf
  libxml-simple-perl libyaml-libyaml-perl libyaml-perl linux-libc-dev linux-libc-dev:armhf make manpages-dev
0 upgraded, 65 newly installed, 0 to remove and 0 not upgraded.
Need to get 77.8 MB of archives.
After this operation, 253 MB of additional disk space will be used.
Do you want to continue? [Y/n] 


gcc 起動の確認

root@jessie:~# arm-linux-gnueabihf-gcc -v
Using built-in specs.
COLLECT_GCC=arm-linux-gnueabihf-gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/arm-linux-gnueabihf/4.9/lto-wrapper
Target: arm-linux-gnueabihf
Configured with: ../src/configure -v --with-pkgversion=' 4.9.2-10' --with-bugurl=file:///usr/share/doc/gcc-4.9/README.Bugs --enable-languages=c,c++,go,fortran,objc --prefix=/usr --program-suffix=-4.9 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.9 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libitm --disable-libquadmath --enable-plugin --with-system-zlib --enable-objc-gc --enable-multiarch --disable-sjlj-exceptions --with-arch=armv7-a --with-fpu=vfpv3-d16 --with-float=hard --with-mode=thumb --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=arm-linux-gnueabihf --program-prefix=arm-linux-gnueabihf-
Thread model: posix
gcc version 4.9.2 ( 4.9.2-10) 
root@jessie:~# 


簡単なプログラムのビルド・テスト

『Hello, World!』的な単純なコードをクロスコンパイルし、MA-E320 実機に転送して実行できるか試してみます。

hello.c
#include <stdio.h>
 
int main(int argc, char **argv)
{
	printf("Hello!\n");
	return 0;
}


ビルドします。

root@jessie:~# arm-linux-gnueabihf-gcc -O2 -Wall hello.c -o hello
root@jessie:~# ls -l hello
-rwxr-xr-x 1 root root 5416 Jan  8 14:30 hello
root@jessie:~# file hello
hello: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 2.6.32, BuildID[sha1]=388d399d609ced3b732f5e7c9e01ff90507754a1, not stripped


MA-E320 実機 に転送します。

root@jessie:~# sftp user1@192.168.253.35
The authenticity of host '192.168.253.35 (192.168.253.35)' can't be established.
ECDSA key fingerprint is 25:33:3b:8d:99:f4:ae:72:88:51:cc:75:52:cd:20:69.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.253.35' (ECDSA) to the list of known hosts.
user1@192.168.253.35's password: 
Connected to 192.168.253.35.
sftp> cd /tmp
sftp> put hello
Uploading hello to /tmp/hello
hello                                                                       100% 5416     5.3KB/s   00:00    
sftp> quit


MA-E320 で実行してみます。

root@plum:/tmp# ls -l hello 
-rwxr-xr-x 1 user1 user1 5416 Jan  8 14:27 hello
root@plum:/tmp# ./hello 
Hello!

単純なプログラムはクロスコンパイラでビルドできました。

様々なライブラリをリンクするプログラムの場合、ライブラリのヘッダや共有ライブラリがリンク時に必要となるため、
このように簡単にはビルドできません。

もし難しいようであれば、実機でのセルフビルドもしくは 『PC上で root filesystem を変更する(QEMU使用)』 でchrootした環境でのビルドをお勧めします。