3

*Update: Regarding the similar question macos-wchar-h-file-not-found, the command line tool switch(xcode-select --install) doesn't exist anymore.

I am running on macOS Monterey system and I am trying to compile a simple hello world .cpp file using the g++-11 commend (I installed gcc using homebrew), I am getting the following error:

/usr/local/Cellar/gcc/11.2.0_3/include/c++/11/cwchar:44:10: fatal error: wchar.h: No such file or directory
   44 | #include <wchar.h>
      |          ^~~~~~~~~

I tried two compile commands:

g++-11 -c hello.cpp -o hell.o 

g++-11 -std=c++11 -c hello.cpp -o hell.o

Here is the file I am trying to compile:

//
//  main.cpp
//  Test
//
//  Created by Jiali Zhu on 1/9/22.
//

#include <iostream>

int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    return 0;
}
Jiali-Z
  • 51
  • 1
  • 5
  • Possible duplicate: https://stackoverflow.com/questions/26185978/macos-wchar-h-file-not-found – Ranoiaetep Jan 11 '22 at 02:38
  • Does this answer your question? [macOS 'wchar.h' File Not Found](https://stackoverflow.com/questions/26185978/macos-wchar-h-file-not-found) – drescherjm Jan 11 '22 at 02:39
  • 1
    @drescherjm I saw that question and tried the things suggested in the answers there. My problem does not seem to be related to xcode's command line tool. – Jiali-Z Jan 11 '22 at 02:45
  • Please include the compile command you used! – Richard Barber Jan 11 '22 at 02:47
  • 2
    @RichardBarber I have made edits to the question. Thank you for the suggestion. Sorry that this is my first time posting questions here. – Jiali-Z Jan 11 '22 at 02:51
  • Command likely needs `-I/usr/local/include` – Richard Barber Jan 11 '22 at 02:54
  • 1
    or, `-I/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk` or something like it with the proper target macos version you have installed. – Richard Barber Jan 11 '22 at 03:00
  • 1
    I believe you're using brew installed gcc, which should have `wchar.h` in `Cellar/gcc/11.2.0_3/include/c++/11/tr1`. If not, you could try to reinstall or check gcc's include path. – halfelf Jan 11 '22 at 04:33

1 Answers1

4

I also had the same problem and in my case the problem was outdated command line developer tools.

I found out it by running

$ brew doctor
...
Warning: Your Command Line Tools are too outdated.
Update them from Software Update in System Preferences or run:
  softwareupdate --all --install --force

If that doesn't show you any updates, run:
  sudo rm -rf /Library/Developer/CommandLineTools
  sudo xcode-select --install

Alternatively, manually download them from:
  https://developer.apple.com/download/all/.
You should download the Command Line Tools for Xcode 13.1.

I tried to update them via softwareupdate -l, however this did not give me any update information.

So removed them and reinstalled with xcode-select --install.

After this g++-11 stopped giving me an error about this header.

Now brew config tells me that I have CLT: 13.0.0.0.1.1627064638

P.S.

Also I started the xcode and it asked me whether additional components need to be installed and I said yes.

tupos
  • 150
  • 8
  • For me remove and re-install did NOT fix the "wchar.h: No such file..." for g++11 compilation. So I suppose you did something else that affected the problem? – Kjell-Olov Högdahl Mar 26 '22 at 17:38
  • What does your brew doctor tells you? Does it give you the error ```Warning: Your Command Line Tools are too outdated. Update them from Software Update in System Preferences or run: softwareupdate --all --install --force If that doesn't show you any updates, run: sudo rm -rf /Library/Developer/CommandLineTools sudo xcode-select --install ``` – tupos Apr 10 '22 at 07:48