更新到 Monterey 后,在 Mac 上使用 gcc 无法编译 C++ 程序

3
*更新: 关于类似问题macos-wchar-h-file-not-found,命令行工具switch(xcode-select --install)不再存在。
我正在运行macOS Monterey系统,并尝试使用g++-11命令(我使用homebrew安装了gcc)编译一个简单的hello world .cpp文件,但是出现以下错误:
/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>
      |          ^~~~~~~~~

我尝试了两个编译命令:

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

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

这是我正在尝试编译的文件:

//
//  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;
}

可能重复问题: https://dev59.com/fF8e5IYBdhLWcg3wAWns - Ranoiaetep
1
@drescherjm 我看到了那个问题并尝试了其中建议的方法。我的问题似乎与Xcode的命令行工具无关。 - Jiali-Z
2
@RichardBarber 我已经编辑了问题。感谢您的建议。很抱歉这是我第一次在这里发布问题。 - Jiali-Z
1
或者,使用正确的目标macOS版本进行安装,类似于-I /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk - Richard Barber
1
我相信你正在使用brew安装的gcc,应该在Cellar/gcc/11.2.0_3/include/c++/11/tr1中有wchar.h。如果没有,你可以尝试重新安装或检查gcc的包含路径。 - halfelf
显示剩余3条评论
1个回答

4

我也遇到了同样的问题,在我的情况下,问题是由于命令行开发工具过期引起的。

我通过运行以下命令找出了问题所在:

$ 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.

我尝试通过softwareupdate -l更新它们,但是没有给我任何更新信息。所以我将其删除并使用xcode-select --install重新安装。之后,g++-11不再报告此头文件的错误。现在,brew config 告诉我我的 CLT: 13.0.0.0.1.1627064638
附注:我还启动了Xcode,并询问是否需要安装其他组件,我选择了“是”。

对我而言,卸载和重新安装并不能解决使用g++11编译时出现的"wchar.h: No such file..."问题。所以我猜您应该采取了其他措施来解决这个问题? - Kjell-Olov Högdahl
你的brew doctor告诉了你什么?它是否给出了以下错误信息?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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接