LoginSignup
5
0

More than 3 years have passed since last update.

mac(Mojave)でbrew gccインストールしたら動かなかったときの解決法

Posted at

前書き

最近C言語のTAをしたりatomからVScodeに乗り換えたりしているので、ついでにC, C++の環境整備をしようと思いその周りのことを調べていると、

g++にすると憧れの#include <bits/stdc++.h>が使えるではないか...!

AtCoderでC++使用してる人が使ってる#include <bits/stdc++.h>

私も使いたーい!(゚Д゚)

起こった問題

いざコンパイルした。

そして出たエラー

ld: warning: ignoring file /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib/libSystem.tbd, file was built for unsupported file format ( 0x2D 0x2D 0x2D 0x20 0x21 0x74 0x61 0x70 0x69 0x2D 0x74 0x62 0x64 0x2D 0x76 0x33 ) which is not the architecture being linked (x86_64): 
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib/libSystem.tbd Undefined symbols for architecture x86_64: "___cxa_atexit", referenced from: __static_initialization_and_destruction_0(int, int) in ccEBCemd.o 
ld: symbol(s) not found for architecture x86_64 collect2: 
error: ld returned 1 exit status

...ライブラリ...?64bit...?(´· ω ·`)
シンボリックリンクは貼ったしフルパスで呼び出しても通らない。
clangでコンパイルは通るからコードが間違ってるわけでもない。
(ちなみにその周りの通常のセッティングについてはこちらの記事にお世話になりました
https://qiita.com/EngTks/items/ffa2a7b4d264e7a052c6#5-stdch)

先に結論

Xcodeアプデしたら治りました。草。

途中過程

3時間だか半日だか四苦八苦しさらに手持ちのproとairの両方試し、挙句これはもう無理だとstack overflowに投げる。
https://stackoverflow.com/questions/58643485/installed-gcc-and-g-on-mac-mojave-is-not-working-brew-install/58687589#58687589

コメントしてくださった方の中である方が、

「sysrootがcommand line tool SDKにセットされてるように見えるからこれ叩いてみて」
g++ --sysroot=/ -o test test.cpp

するとエラー内容が変わった。
取っとくの忘れてしまったんだけど大体こんな感じだった。

fatal error: wchar.h: No such file or directory
 #include <wchar.h>
          ^~~~~~~~~

....標準ライブラリがどっかいった?

その証拠にライブラリinclude無しで

int main(){}

のコンパイルを試してみるとほぼ通る。

...あ、ライブラリですね。

するとこちらはよく起きてる問題のようで、どうもMojaveだと/usr/include辺りか標準ライブラリがどっかいってしまってるらしい。(えっ辛い)
(最近のパッチでは直ってるのかもしれない)

参考:
https://qiita.com/tamutamuta/items/0ad8a11fe5d195aaffcb
https://teratail.com/questions/169308

この標準ライブラリどっかいっちゃったよ問題を解決するにはXcodeやgccを再インストールすることらしい。
私の場合はアプデで治りました。(四苦八苦してる時にgcc入れ直したけど)

え?普段からアプデしとかないのが悪い?
すいません... (´· ω ·`)

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