1

After I upgraded my macos to 12.2.1, I am getting weird errors when I run g++ (also with gcc). Here is a sample:

In file included from /usr/local/Cellar/gcc/10.2.0_2/include/c++/10.2.0/bits/postypes.h:40,
             from /usr/local/Cellar/gcc/10.2.0_2/include/c++/10.2.0/iosfwd:40,
             from /usr/local/Cellar/gcc/10.2.0_2/include/c++/10.2.0/ios:38,
             from /usr/local/Cellar/gcc/10.2.0_2/include/c++/10.2.0/ostream:38,
             from /usr/local/Cellar/gcc/10.2.0_2/include/c++/10.2.0/iostream:39,
             from try1.cpp:2:
/usr/local/Cellar/gcc/10.2.0_2/include/c++/10.2.0/cwchar:44:10: fatal error: wchar.h: No such file or directory
   44 | #include <wchar.h>
      |          ^~~~~~~~~

I tried the following solutions, but none of them have worked so far:

(A) updated xcode: (same result)

xcode-select --install

(B) based on solution suggested here:

export CPATH=/Library/Developer/CommandLineTools/usr/include/c++/v1

This only changed the line number of the error, now I get this error message:

In file included from /Library/Developer/CommandLineTools/usr/include/c++/v1/iosfwd:95,
             from /Library/Developer/CommandLineTools/usr/include/c++/v1/ios:214,
             from /Library/Developer/CommandLineTools/usr/include/c++/v1/iostream:37,
             from try1.cpp:2:
/Library/Developer/CommandLineTools/usr/include/c++/v1/wchar.h:119:15: fatal error: wchar.h: No such file or directory
  119 | #include_next <wchar.h>
      |               ^~~~~~~~~

When I searched for this file, I saw this is at:

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/wchar.h

For your reference, this is the simple code that I am trying to compile:

#include <iostream>
#include <string>

using namespace std;

int main () {

   string str1 = "Hello";
   string str3;

   // copy str1 into str3
   str3 = str1;
   cout << "str3 : " << str3 << endl;
}

And my xcode version is:

Xcode 13.2.1
Build version 13C100

(I get similar problems when I run gcc, example: "_stdio.h not found")

Any suggestions on how to fix this problem?

R71
  • 4,283
  • 7
  • 32
  • 60
  • Do I need to uninstall and re-install xcode and gcc? – R71 Mar 05 '22 at 14:47
  • Just out of curiosity: If you instead `export CPATH=/Library/Developer/CommandLineTools/usr/include` and `export CPLUS_INCLUDE_PATH=/Library/Developer/CommandLineTools/usr/include/c++/v1` , do you still get the same error? – Ted Lyngmo Mar 05 '22 at 14:55
  • @TedLyngmo: yes, exactly the same error as with the export of CPATH – R71 Mar 05 '22 at 15:23
  • Ok, it was worth a try. If you also add `export C_INCLUDE_PATH=/Library/Developer/CommandLineTools/usr/include`, same thing I guess? – Ted Lyngmo Mar 05 '22 at 15:29
  • There is a lot of answers on SO. One of them should help. https://stackoverflow.com/questions/26185978/macos-wchar-h-file-not-found https://stackoverflow.com/questions/46342411/wchar-h-file-not-found – 273K Mar 05 '22 at 15:43
  • @TedLyngmo: yes, same error with that also – R71 Mar 05 '22 at 16:29
  • @273K: many of those answers refer to SDK. Are those for some app development packages? Should I try different paths for normal (not app building) compilation? – R71 Mar 05 '22 at 16:33

2 Answers2

0

I'm on 12.2.1. I created a very simple file:

#include <wchar.h>

int main() {
}

And I compiled with:

g++ Foo.cpp --std=c++17 -o Foo

It compiled without any errors. For me, this is a brand new machine (I literally got it yesterday). I have none of the env vars included you list.

I get the g++ in /usr/bin/g++. However, it's not the gnu compiler. It's Apple's clang. (g++ --version). clang version 13.0.0.

If you upgraded and did the xcode thing, then you might be in a weird mode that you have real g++ in your path, but it's pointing to weird shit. If you need g++ and not clang, then maybe remove and reinstall.

Joseph Larson
  • 8,530
  • 1
  • 19
  • 36
0

I just faced that when my Mac decided to auto update the XCode version. I solve it by updating (reinstalling) XCode command line tools.

sudo rm -rf /Library/Developer/CommandLineTools
xcode-select --install

Reference: How to update Xcode from command line

  • Yes, looks like that is the only solution. I also reinstalled my brew, just to be on the safe side. – R71 May 03 '22 at 05:54