MacOS Big SurにHomebrewでgccコンパイラをインストールしたときの作業メモです。
2021年9月28日に作業しました。
C言語の開発環境構築
xcodeから?
ターミナルでgccと打つと、xcodeのポップアップが表示されて、あとはボタンポチポチしてればインストールできるらしい。
インストールが完了すると、gccという実行ファイルが/usr/bin/に置かれる;
% which gcc /usr/bin/gcc
しかしこのgccコマンドは、実際は(gccではなく?)clangというコンパイラが呼ばれるらしい;
% gcc -v Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1 Apple clang version 13.0.0 (clang-1300.0.29.3) Target: x86_64-apple-darwin20.6.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
本物の?gccを使いたい場合は、下記のようにHomebrewでインストールするのがよさげ。
Homebrew
下記コマンドでgccをインストール;
brew install gcc
Homebrewでインストールしたパッケージは/usr/local/Cellar/に置かれる。
また、シンボリックリンクが/usr/local/bin/に置かれる;
% ls -go /usr/local/bin/gcc* lrwxr-xr-x 1 31 9 28 00:14 /usr/local/bin/gcc-11@ -> ../Cellar/gcc/11.2.0/bin/gcc-11 lrwxr-xr-x 1 34 9 28 00:14 /usr/local/bin/gcc-ar-11@ -> ../Cellar/gcc/11.2.0/bin/gcc-ar-11 lrwxr-xr-x 1 34 9 28 00:14 /usr/local/bin/gcc-nm-11@ -> ../Cellar/gcc/11.2.0/bin/gcc-nm-11 lrwxr-xr-x 1 38 9 28 00:14 /usr/local/bin/gcc-ranlib-11@ -> ../Cellar/gcc/11.2.0/bin/gcc-ranlib-11
gcc-11というのが、バージョン11のgccコマンド。
/usr/local/bin/にはPATHが通っているはずなので、ターミナルでgcc-11と打つだけで実行できる;
% gcc-11 -v Using built-in specs. COLLECT_GCC=gcc-11 COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc/11.2.0/libexec/gcc/x86_64-apple-darwin20/11.2.0/lto-wrapper Target: x86_64-apple-darwin20 Configured with: ../configure --prefix=/usr/local/Cellar/gcc/11.2.0 --libdir=/usr/local/Cellar/gcc/11.2.0/lib/gcc/11 --disable-nls --enable-checking=release --enable-languages=c,c++,objc,obj-c++,fortran,d --program-suffix=-11 --with-gmp=/usr/local/opt/gmp --with-mpfr=/usr/local/opt/mpfr --with-mpc=/usr/local/opt/libmpc --with-isl=/usr/local/opt/isl --with-zstd=/usr/local/opt/zstd --with-pkgversion='Homebrew GCC 11.2.0' --with-bugurl=https://github.com/Homebrew/homebrew-core/issues --enable-libphobos --build=x86_64-apple-darwin20 --with-system-zlib --disable-multilib --without-build-config --with-native-system-header-dir=/usr/include --with-sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 11.2.0 (Homebrew GCC 11.2.0)
毎回gcc-11と打つのがめんどくさいなら、エイリアスを設定すればいい。
エディタで~/.zshrcを開いて、エイリアスを追加;
alias gcc='gcc-11'
設定ファイルを再読み込みする;
source ~/.zshrc
そうすると、gccと打つだけでgcc-11が実行される;
% gcc -v Using built-in specs. COLLECT_GCC=gcc-11 COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc/11.2.0/libexec/gcc/x86_64-apple-darwin20/11.2.0/lto-wrapper Target: x86_64-apple-darwin20 Configured with: ../configure --prefix=/usr/local/Cellar/gcc/11.2.0 --libdir=/usr/local/Cellar/gcc/11.2.0/lib/gcc/11 --disable-nls --enable-checking=release --enable-languages=c,c++,objc,obj-c++,fortran,d --program-suffix=-11 --with-gmp=/usr/local/opt/gmp --with-mpfr=/usr/local/opt/mpfr --with-mpc=/usr/local/opt/libmpc --with-isl=/usr/local/opt/isl --with-zstd=/usr/local/opt/zstd --with-pkgversion='Homebrew GCC 11.2.0' --with-bugurl=https://github.com/Homebrew/homebrew-core/issues --enable-libphobos --build=x86_64-apple-darwin20 --with-system-zlib --disable-multilib --without-build-config --with-native-system-header-dir=/usr/include --with-sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 11.2.0 (Homebrew GCC 11.2.0)
whereやwhichで確認;
% where gcc gcc: aliased to gcc-11 /usr/bin/gcc % which gcc gcc: aliased to gcc-11
C言語のコンパイルテスト
下記コードをtest.cとして保存;
#include<stdio.h> #include<math.h> #include<stdlib.h> int main(){ double pi = 2 * asin(1); printf("hello C!\n"); printf("pi = %f\n", pi); return 0; }
コンパイル&実行;
# コンパイル % gcc test.c # 実行 % ./a.out hello C! pi = 3.141593
以上です。