所以,我想使用一些 GNU C++ 原生的头文件:
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
我读到在 MacOS 中,gcc 和 g++ 都链接到 clang。因此,我们必须使用 homebrew 安装 gcc 并使用它。 但是使用 homebrew 安装 gcc 后。当我跑步时
g++ --version
我明白了
Apple clang version 12.0.5 (clang-1205.0.22.9)
Target: arm64-apple-darwin22.1.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
但是跑步
g++-12 --version
我得到:
g++-12 (Homebrew GCC 12.2.0) 12.2.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
我的 VSCode 运行 g++ (Apple One) 来编译 C/C++ 文件。为了我一开始想要实现的目标,我读到我们需要让 g++(使用自制程序安装)进行编译。
因此,我运行了以下命令:
cd /opt/homebrew/bin
ls -s g++-12 g++
但是现在,即使我编译以下代码:
#include <iostream>
int main()
{
std::cout << 1;
}
我收到以下错误:
In file included from /opt/homebrew/Cellar/gcc/12.2.0/include/c++/12/bits/postypes.h:40,
from /opt/homebrew/Cellar/gcc/12.2.0/include/c++/12/iosfwd:40,
from /opt/homebrew/Cellar/gcc/12.2.0/include/c++/12/ios:38,
from /opt/homebrew/Cellar/gcc/12.2.0/include/c++/12/ostream:38,
from /opt/homebrew/Cellar/gcc/12.2.0/include/c++/12/iostream:39,
from test.cpp:1:
/opt/homebrew/Cellar/gcc/12.2.0/include/c++/12/cwchar:44:10: fatal error: wchar.h: No such file or directory
44 | #include <wchar.h>
| ^~~~~~~~~
compilation terminated.
现在,使用
rm g++
删除链接将恢复到我的原始配置。但该配置无法运行我一开始请求的标头。有什么办法可以解决这个问题吗
以上大部分内容大部分摘自这里。但我没有找到任何解决办法。但是,我看到人们使用相同的方法并且取得了成功。
编辑: 我找到了一个网站,那里有解决方案。这是不使用
g++
进行编译。还不如用
g++-12 --sysroot=$(xcrun --show-sdk-path)
当我使用这个时,它解决了这个问题。有人可以解释为什么会发生这种情况吗?
标头错误可能表明您通过 Homebrew 安装的
g++
可能与 macos 系统目录中安装的 Apple Xcode 版本不兼容。
解决方案可能是重新安装一个或两个软件包。
编辑:
g++-12 --sysroot=$(xcrun --show-sdk-path)
将系统头文件包含的搜索路径从默认值(可能是在自制程序安装 g++ 时设置的)更改为当前安装的 Xcode SDK 提供的路径。
简短的答案是在
/usr/local/bin
中创建符号链接,因为 Mac 当前不允许 直接在 /usr/bin
中更改符号链接
首先创建/usr/local/bin
mkdir -p /usr/local/bin
cd /usr/local/bin
通过 gcc-
上的选项卡查找可用的 gcc 版本sudo ln -s /opt/homebrew/bin/g++-14 g++
sudo ln -s /opt/homebrew/bin/gcc-14 gcc
添加 .zshrc 或 .bashrc 的路径
export PATH="/usr/local/bin:$PATH"
重启终端
zsh