LoginSignup
0
0

Macシリコンでクロスコンパイラ

Posted at

Macシリコンでクロスコンパイラを扱う

クロスコンパイラで有名どころの「crosstool-ng」を、Macシリコンでトライしたときの記録。

crosstool-ngのインストール

Macにcrosstool-ngを利用するためには、まず、開発環境を整える必要がある。下記をインストール(詳細略)。

  • Xcodeコマンドラインツール
  • パッケージマネージャーのHomebrew

そして、crosstool-ngのインストール。

$ sudo brew install crosstool-ng

クロスコンパイラの作成

つまづき

crosstool-ngでは、大文字小文字を区別する必要があるとのこと。そのまま、Buildしようとすると、ログに下記メッセージが表示され、Buildがストップする。

[ERROR]  Your file system in '/Users/xxxyyzz/proj/crosstool-ng/rpi/.build' is *not* case-sensitive!

MacのデフォルトファイルシステムAPFSでは、大文字小文字を区別しないらしい。そこで、この記事などの情報から、ディスクユーティリティーで、大文字小文字を区別するAPFS内コンテナを作成した。
DiskUtilityForCross.png

/Volumes/ForCross

上記Path配下で、crosstool-ngを実行する。

ターゲットはx64(インテルバイナリ)

ここからは、過去の小生の記事「意味のないクロスコンパイラ」とほぼ同じ。

$ cd /Volumes/ForCross
$ mkdir x64
$ cd x64
$ ct-ng x86_64-unknown-linux-gnu
$ ct-ng menuconfig

menuconfigにて、パスを適宜変更する。
menuconfigのコピー.png
その他、必要に応じて、configの内容を修正しBuild。

$ ct-ng build

結果、下記Pathにクロスコンパイラができあがる。

/Volumes/ForCross/x64/x-tools/x86_64-unknown-linux-gnu/bin

実行

ターゲットソースコード

$ cat test.c
#include <stdio.h>
int main()
{
	(void)printf("Test\n");
	return 0;
}

クロスコンパイル

% /Volumes/ForCross/x64/x-tools/x86_64-unknown-linux-gnu/bin/x86_64-unknown-linux-gnu-gcc test.c -o x64

% file x64
x64: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, with debug_info, not stripped

% ./x64
zsh: exec format error: ./x64

x64バイナリが作られている。当然のことながら、Mac(ARM)環境では実行不可。

x64(Linux)環境下での実行

x64配下にコピーし実行。

$ file x64
x64: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, with debug_info, not stripped
$ ./x64
Test

当然、実行できる。

おまけ(実行時間)

小生のMac(CPU:Apple M2, 24GB memory)でのいくつかのターゲットの実行(コンパイラ作成)時間は下記となった。

x86_64-unknown-linux-gnu armv7-rpi2-linux-gnueabihf x86_64-multilib-linux-gnu
19分15秒 18分00秒 25分00秒

まずまずの時間であろうか。

EOF

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0