R.Minami

macOSでgccを使う

2022.08.08

macOSでGCCを使ってコンパイル・実行させる。

環境

手順

brewからgccをインストール

以下のコマンドでgccをインストール。

                    
$ brew install gcc
                    
                

/usr/local/binにあるgccを見ると
筆者のgccのバージョンは11でした

                        
gcc-11
g++-11
                        
                    

リンク

デフォルトだとXCodeのClang?がgcc/g++がリンクされているっぽい。

なので、gcc/g++のリンクを削除してからリンクを作成する。

                    
$ rm /usr/local/bin/gcc
$ rm /usr/local/bin/g++
$ ln -s /usr/local/bin/gcc-11 /usr/local/bin/gcc
$ ln -s /usr/local/bin/g++-11 /usr/local/bin/g++
                    
                

リンクされているか確認

コマンドを叩いて確認してみる

                    
$ gcc --version
gcc (Homebrew GCC 11.3.0_2) 11.3.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
                        
$ g++ --version
g++ (Homebrew GCC 11.3.0_2) 11.3.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
                    
                

これでリンクが通ってる。

コンパイルと実行

コンパイルと実行してみる

Test.cpp

                    
#include <bits/stdc++.h>

using namespace std;

int main()
{
    cout << "Hello World!" << endl;
    return 0;
}
                    
                
                    
$ g++ Test.cpp
$ ./a.out
Hello World!