0

I am using MacOS 10.15. Since the clang shipped with MacOS does not include clang-format. I installed another pre-built clang binary from here. I have added the binary file path to my PATH variable.

export PATH="$HOME/tools/clang+llvm-10.0.0-x86_64-apple-darwin/bin:$PATH"

I tried to compile a simple program:

#include <iostream>

int main(int argc, char *argv[]) {
    std::cout << "Hello world!\n";
    return 0;
}

using the following command:

clang++ hello.cpp -o hello

I got the following error:

In file included from hello.cpp:1:
In file included from /Users/jdhao/tools/clang+llvm-10.0.0-x86_64-apple-darwin/bin/../include/c++/v1/iostream:37:
In file included from /Users/jdhao/tools/clang+llvm-10.0.0-x86_64-apple-darwin/bin/../include/c++/v1/ios:214:
In file included from /Users/jdhao/tools/clang+llvm-10.0.0-x86_64-apple-darwin/bin/../include/c++/v1/iosfwd:95:
/Users/jdhao/tools/clang+llvm-10.0.0-x86_64-apple-darwin/bin/../include/c++/v1/wchar.h:118:15: fatal error: 'wchar.h' file not found
#include_next <wchar.h>
              ^~~~~~~~~
1 error generated.

I found that wchar.h bundled with this pre-built package is in the following directory:

/Users/jdhao/tools/clang+llvm-10.0.0-x86_64-apple-darwin/include/c++/v1/

So I added the -I flag:

clang++ -I /Users/jdhao/tools/clang+llvm-10.0.0-x86_64-apple-darwin/include/c++/v1 hello.cpp -o hello 

The error still persists.

If I use clang++ shipped with MacOS, I have no problem compiling the source code:

# the following works without any error
/usr/bin/clang++ hello.cpp -o hello

I have seen post here, here, and here, but the solutions do not apply.

jdhao
  • 24,001
  • 18
  • 134
  • 273

1 Answers1

1

You got clang-format improperly. Reset the system to the state before you installed another pre-built clang binary. Then use Homebrew to install clang-format

brew install clang-format

clang+llvm-10.0.0-x86_64-apple-darwin is not suitable to your Mac. It depends on system frameworks that are not available, so you get the error finding wchar.h in a system framework. When you install clang+llvm-10.0.0-x86_64-apple-darwin you ignore framework dependencies. Homebrew will care about dependencies.

273K
  • 29,503
  • 10
  • 41
  • 64