LoginSignup
5
4

More than 5 years have passed since last update.

Mojaveにしたらclangが動かないのを解決した

Posted at

Apple clangから普通のclangに切り替えようとしたとき、clangがエラーを吐いてコンパイルできなくなってしまった。
macOSをMojaveにアップデートしたら起きるmacOS特有のエラーらしく、記事のとおりに試したが解決できずにいたのだが、その原因がわかり解決できたので、今回はその備忘録的なことを書く。

概要

Emacsのironyを動かしたくて、Apple clangからclangに切り替え。

brew install llvm

そのあとパスを設定。

だが、EmacsのironyがC++のメンバ関数の補完が動かない…
どういう状況かというと、例えば、下記のようなC++のコードがあったとき、

#include<iostream>
#include<vector>
using namespace std;

int main(int argc, char const *argv[])
{
    vector<int> v;
    return 0;
}

v.pと入力すればv.push_back()と補完されるはずだが、何故かされない…
変数やクラス名の補完は動いているのでEmacsのせいでは無いみたい…

clangのせいでは?

あれ?と思いclangでコンパイルしたら、

$ clang++ Test.cpp
In file included from Test.cpp:1:
In file included from /usr/local/Cellar/llvm/7.0.1/include/c++/v1/iostream:38:
In file included from /usr/local/Cellar/llvm/7.0.1/include/c++/v1/ios:215:
In file included from /usr/local/Cellar/llvm/7.0.1/include/c++/v1/iosfwd:90:
/usr/local/Cellar/llvm/7.0.1/include/c++/v1/wchar.h:119:15: fatal error: 'wchar.h' file not found
#include_next <wchar.h>
              ^~~~~~~~~
1 error generated.

なるほど…<wchar.h>がないから動かない…と怒られてしまった。
原因はxcodeのCommandLineToolsが入ってないかららしい。

xcode-select --install
sudo xcode-select --switch /Library/Developer/CommandLineTools/

もう一度

$ clang++ Test.cpp
In file included from Test.cpp:1:
In file included from /usr/local/Cellar/llvm/7.0.1/include/c++/v1/iostream:38:
In file included from /usr/local/Cellar/llvm/7.0.1/include/c++/v1/ios:215:
In file included from /usr/local/Cellar/llvm/7.0.1/include/c++/v1/iosfwd:90:
/usr/local/Cellar/llvm/7.0.1/include/c++/v1/wchar.h:119:15: fatal error: 'wchar.h' file not found
#include_next <wchar.h>
              ^~~~~~~~~
1 error generated.

動かない…
調査してみたら下記の記事が見つかった。

Xcode 10 Release Notes

Install failed, "zlib not available" on macOS Mojave

これらによるとMojaveではxcode-select —installしてもソフトウェア(?)によってはヘッダファイルのインストールが失敗するみたい

sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /

結果

$ clang++ Test.cpp
$ ./a.out
やったぜ

使用したコード

Text.cpp
#include<iostream>
#define cout(x) cout << x << endl;
using namespace std;

int main(int argc, char const *argv[])
{
    cout("やったぜ")
    return 0;
}
5
4
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
4