しまてく

学んだ技術を書きためるブログ

MacOSXでAndroid向けにC++をクロスコンパイルする

最終的にうまく行ったやりかた

以下のモジュールをインストール。
sudo port install gmp
sudo port install mpfr
sudo port install mpc
sudo port install ppl
sudo port install arm-none-linux-gnueabi-binutils
sudo port install arm-none-linux-gnueabi-gcc
ロスコンパイルする。
arm-none-linux-gnueabi-g++ --static -o helloworld ./helloworld.cpp
helloworld.cppの中身。
#include <stdio.h>

int main() {
	printf("Hello World!\n");
	return 0;
}
fileコマンドで見てみる。
file helloworld

helloworld: ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, for GNU/Linux 2.4.17, not stripped
実機に転送
adb push helloworld /data/tmp

※実機の「/data/tmp」はあらかじめ作ってあり、ユーザーに書き込み権限があります。

実機で実行
adb shell
$ cd /data/tmp
$ ./sample
Hello World!

ふむ。

試行錯誤

初め「arm-elf-gcc」を使っていたのですが、これがまずかったようでリンク先の3にあるように「[1] Segmentation fault」が出てしまいました。

これを「arm-none-linux-gnueabi-gcc」を使うことでちゃんと実行できるようになったのでオーライですね。

以上。