Homebrew Clang C++ compilation fails

59 Views Asked by At

I am having a similar issue as here, but I am opening a new question -- while the symptom is the same, my goal is different. In that question, removing llvm-clang solved their issue -- I assume the default Apple clang fit their needs. In my situation, I am intentionally trying to install and use Homebrew clang, not Apple's default. I am on macOS Sonoma and want the upgraded compiler to access C++23 language features (such as std::print).

My default version of Apple Clang (by running clang --version) is 14:

$ clang --version
Apple clang version 14.0.3 (clang-1403.0.22.14.1)
Target: arm64-apple-darwin23.1.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

I installed the latest version of llvm using brew install llvm, which installs version 17. Following the tips here:

I modified my ~./zshrc to add the updated llvm to PATH, as well as changing the CC and CXX variables to expose the new compiler to CMake:

PATH="/opt/homebrew/Cellar/llvm/17.0.6_1/bin${PATH:+:${PATH}}"; export PATH;
export CC=/opt/homebrew/Cellar/llvm/17.0.6_1/bin/clang
export CXX=/opt/homebrew/Cellar/llvm/17.0.6_1/bin/clang++

With these changes in effect, I can verify that clang points to the right version now:

$ clang --version
Homebrew clang version 17.0.6
Target: arm64-apple-darwin23.1.0
Thread model: posix
InstalledDir: /opt/homebrew/Cellar/llvm/17.0.6_1/bin

Now, to compile a simple program:

//main.cpp

#include <iostream>

int main() { std::cout << "Hello world" << '\n'; }

$ clang -std=c++23 main.cpp -ferror-limit=10
In file included from main.cpp:52:
In file included from /opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/iostream:43:
In file included from /opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/ios:221:
In file included from /opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/__ios/fpos.h:14:
In file included from /opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/iosfwd:106:
In file included from /opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/__std_mbstate_t.h:14:
/opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/__mbstate_t.h:51:5: error: "We don't know how to get the definition of mbstate_t without <wchar.h> on your platform."
   51 | #   error "We don't know how to get the definition of mbstate_t without <wchar.h> on your platform."
      |     ^
In file included from main.cpp:52:
In file included from /opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/iostream:43:
In file included from /opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/ios:221:
In file included from /opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/__ios/fpos.h:14:
/opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/iosfwd:123:14: error: reference to unresolved using declaration
  123 | typedef fpos<mbstate_t>    streampos;
      |              ^
/opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/__std_mbstate_t.h:25:1: note: using declaration annotated with 'using_if_exists' here
   25 | using ::mbstate_t _LIBCPP_USING_IF_EXISTS;
      | ^
In file included from main.cpp:52:
In file included from /opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/iostream:43:
In file included from /opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/ios:221:
In file included from /opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/__ios/fpos.h:14:
/opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/iosfwd:123:14: error: reference to unresolved using declaration
  123 | typedef fpos<mbstate_t>    streampos;
      |              ^
/opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/__std_mbstate_t.h:25:1: note: using declaration annotated with 'using_if_exists' here
   25 | using ::mbstate_t _LIBCPP_USING_IF_EXISTS;
      | ^
In file included from main.cpp:52:
In file included from /opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/iostream:43:
In file included from /opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/ios:221:
In file included from /opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/__ios/fpos.h:14:
/opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/iosfwd:124:14: error: reference to unresolved using declaration
  124 | typedef fpos<mbstate_t>    wstreampos;
      |              ^
/opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/__std_mbstate_t.h:25:1: note: using declaration annotated with 'using_if_exists' here
   25 | using ::mbstate_t _LIBCPP_USING_IF_EXISTS;
      | ^
In file included from main.cpp:52:
In file included from /opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/iostream:43:
In file included from /opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/ios:221:
In file included from /opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/__ios/fpos.h:14:
/opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/iosfwd:124:14: error: reference to unresolved using declaration
  124 | typedef fpos<mbstate_t>    wstreampos;
      |              ^
/opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/__std_mbstate_t.h:25:1: note: using declaration annotated with 'using_if_exists' here
   25 | using ::mbstate_t _LIBCPP_USING_IF_EXISTS;
      | ^
In file included from main.cpp:52:
In file included from /opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/iostream:43:
In file included from /opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/ios:221:
In file included from /opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/__ios/fpos.h:14:
/opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/iosfwd:126:14: error: reference to unresolved using declaration
  126 | typedef fpos<mbstate_t>    u8streampos;
      |              ^
/opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/__std_mbstate_t.h:25:1: note: using declaration annotated with 'using_if_exists' here
   25 | using ::mbstate_t _LIBCPP_USING_IF_EXISTS;
      | ^
In file included from main.cpp:52:
In file included from /opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/iostream:43:
In file included from /opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/ios:221:
In file included from /opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/__ios/fpos.h:14:
/opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/iosfwd:126:14: error: reference to unresolved using declaration
  126 | typedef fpos<mbstate_t>    u8streampos;
      |              ^
/opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/__std_mbstate_t.h:25:1: note: using declaration annotated with 'using_if_exists' here
   25 | using ::mbstate_t _LIBCPP_USING_IF_EXISTS;
      | ^
In file included from main.cpp:52:
In file included from /opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/iostream:43:
In file included from /opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/ios:221:
In file included from /opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/__ios/fpos.h:14:
/opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/iosfwd:128:14: error: reference to unresolved using declaration
  128 | typedef fpos<mbstate_t>    u16streampos;
      |              ^
/opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/__std_mbstate_t.h:25:1: note: using declaration annotated with 'using_if_exists' here
   25 | using ::mbstate_t _LIBCPP_USING_IF_EXISTS;
      | ^
In file included from main.cpp:52:
In file included from /opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/iostream:43:
In file included from /opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/ios:221:
In file included from /opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/__ios/fpos.h:14:
/opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/iosfwd:128:14: error: reference to unresolved using declaration
  128 | typedef fpos<mbstate_t>    u16streampos;
      |              ^
/opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/__std_mbstate_t.h:25:1: note: using declaration annotated with 'using_if_exists' here
   25 | using ::mbstate_t _LIBCPP_USING_IF_EXISTS;
      | ^
In file included from main.cpp:52:
In file included from /opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/iostream:43:
In file included from /opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/ios:221:
In file included from /opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/__ios/fpos.h:14:
/opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/iosfwd:129:14: error: reference to unresolved using declaration
  129 | typedef fpos<mbstate_t>    u32streampos;
      |              ^
/opt/homebrew/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/__std_mbstate_t.h:25:1: note: using declaration annotated with 'using_if_exists' here
   25 | using ::mbstate_t _LIBCPP_USING_IF_EXISTS;
      | ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
11 errors generated.

The test program isn't using any C++23 features, so this seems like a more fundamental config issue with llvm-clang but I may be wrong. Does anybody have suggestions?

0

There are 0 best solutions below