5
3

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 3 years have passed since last update.

M1 MacにRosetta2なしでGCC環境を構築する

Last updated at Posted at 2022-03-13

こんにちは。2022年3月初投稿です。

はじめに

先日GCCをM1 Macにインストールしたのですが、
IntelからAppleSiliconへ切り替わる過渡期なため、インストール手順を紹介した記事が

  • Intel Mac向け
  • AppleSilicon向け(Rosetta2あり)
  • AppleSilicon向け(Rosetta2なし)

の3パターンあり、すこし混乱しました。
しかも関連ソフトも同じ有様で、わかりづらかったので
備忘録的にまっさらなAppleSilicon搭載MacにRosseta2なしでGCC環境を構築する手順をまとめていきます。

環境

マシン: Mac mini (M1, 2020)
OS: macOS 12.2(Monterey)
シェル: zsh

関連ソフトのインストール

CommandLineTools for Xcode

開発者向けツールのCommandLineTools for Xcodeをインストールします。
(Xcodeを使う予定がある場合は、そちらのインストールで代替可能)

% xcode-select --install

Homebrewのインストール

パッケージ管理ツールのHomebrewをインストールします。
公式サイトのインストール用のコマンドを実行します。パスワードや了承を求められるので指示に従って操作します。
homebrew.png

インストール後に案内が出てきますが、M1 Mac(AppleSilicon搭載Mac)の場合は以下のコマンドを実行してパスを通します。

% echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/******/.zprofile
# ******はユーザ名に置き換え
% eval "$(/opt/homebrew/bin/brew shellenv)"

Homebrewのバージョン確認コマンドを実行して、インストールが無事完了したか確認します。

% brew --version
実行結果例
Homebrew 3.4.1
Homebrew/homebrew-core (git revision 7aa6ef8a215; last commit 2022-03-12)

GCCのインストール

Homebrewを利用してGCCをインストールします。

% brew install gcc

GCCのコマンドを呼び出せるように設定していきます。

% cd /opt/homebrew/bin/
% ln -s gcc-11 gcc
% where gcc
実行結果例
/opt/homebrew/bin/gcc
/usr/bin/gcc

G++も同様に設定します。

% ln -s g++-11 g++
% where g++
実行結果例
/opt/homebrew/bin/g++
/usr/bin/g++

Macを再起動して終了です。

動作確認

適当なC/C++プログラムをコンパイル・実行して、動作を確認します。

hello.c
#include <stdio.h>

int main(void){
	printf("Hello GCC World!\n");
}
実行結果例
% gcc hello.c
% ./a.out
Hello GCC World!

さいごに

Intel Macで環境構築をしたときの手順を試みたら、Homebrewの段階でつまづいてしまい、
ググるとIntel Macしかなかった時代(PowerPC?知らない子ですね)の記事やら、
M1黎明期のRosseta2を使った方法が出てきて、余計に混乱しました。
Rosseta2を使わずにすむならその方がうれしいので、HomebrewからGCCのインストールまでの一連の流れをまとめてみました。
そんなことより新型Mac miniまだですか。

参考

5
3
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
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?