Can't compile c++ program using gcc on Mac after updating to Monterey

Viewed 2331

*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;
}
1 Answers

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.

Related