0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

macOS で C++ からラズパイ用のバイナリ作る話 (zig)

Posted at

これは何?

macOS 上で ラズパイバイナリを作るに当たり。
zig で作ったら失敗したよ、という話と、簡単だったよ、という話。

まずは失敗した話

$ zig version
0.10.1
$ zig c++ -std=c++20 -Wall --target=arm-linux-gnueabi main.cpp
warning(compilation): libc++ does not work on multi-threaded ARM yet.
For more details: https://github.com/ziglang/zig/issues/6573
error: unable to create compilation: TargetRequiresSingleThreaded

などとなり、失敗する。

arm-linux-gnueabihf でも arm-linux-musleabihf でもthumb-linux-musleabi でも同様。

残念。

そして成功した話

ふと。
ARM64 Linux だったら行けるのかも。
と思って試してみた。

$ zig version
0.10.1
$ zig c++ -std=c++20 -O2 -Wall --target=aarch64-linux-musl main.cpp
$ zig c++ -std=c++20 -O2 -Wall --target=aarch64-linux-gnu main.cpp

どちらも成功。
ラズパイ上で実行したら、ちゃんと動いた。今のところなんの問題もない。

std::thread などを使ってもちゃんと動く。

いいこと

tool chain の整備が簡単。

brew install zig だけで手に入る。簡単。

ビルドが簡単

例えばソースコードが foo.cpp bar.cpp baz.cpp の3つなら、

$ zig c++ 略 foo.cpp bar.cpp baz.cpp

と、書けばよい。

「略」の部分は -std=c++20 -O2 -Wall --target=aarch64-linux-musl -I hoge/include など。

これで makefile 経由でのビルド同様、オブジェクトファイルよりソースコードが新しければオブジェクトを作り直すし、そうじゃなければ最新のものを使う、とういことになる。

--target= の指定を変えれば Windows 版も macOS on AMD64 版も ARM64 Linux 版も作れる。
なぜか 32bit ARM Linux 版は作れないけれど。

感想

ラズパイ 3B+ や ラズパイ4 を使う場合、これを理由に 64bit OS を選ぼうと思う。

なにか事情がある場合は仕方ないけど、64bit Linux をファーストチョイスにする。

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?