[英]How to configure g++ as the default compiler in Mac OS (M1)
So, I wanted to use some header files native to GNU C++:所以,我想使用 GNU C++ 原生的一些 header 文件:
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
I read that in MacOS, gcc and g++ are both linked to clang. So, we had to install the gcc using homebrew and use that.我读到在 MacOS 中,gcc 和 g++ 都链接到 clang。因此,我们必须使用自制软件安装 gcc 并使用它。 But after installing gcc using homebrew.
但是在使用自制软件安装 gcc 之后。 When I run
当我跑步时
g++ --version
I get我明白了
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
But running g++-12 --version
I get:但是运行
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.
My VSCode runs g++ (Apple One) to compile C/C++ files.我的 VSCode 运行 g++ (Apple One) 来编译 C/C++ 文件。 For the goal I wanted to accomplish at the start, I read that we need to have the g++ (installed using homebrew) do the compiling.
对于我一开始想要完成的目标,我读到我们需要让 g++(使用自制软件安装)进行编译。
So, I ran the following commands:所以,我运行了以下命令:
cd /opt/homebrew/bin
ls -s g++-12 g++
But now, even when I compile the following code:但是现在,即使我编译以下代码:
#include <iostream>
int main()
{
std::cout << 1;
}
I get the following error:我收到以下错误:
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.
Now, removing the link using rm g++
reverts back to my original configuration.现在,使用
rm g++
删除链接会恢复到我的原始配置。 But that configuration can't run the headers I requested at the start.但是该配置无法运行我在开始时请求的标头。 Is there any way to solve this?
有什么办法可以解决这个问题吗?
Most of the above have mostly been taken from here .以上大部分内容大多取自此处。 But I didn't find any solution.
但我没有找到任何解决方案。 But, I have seen people using the same and getting successful .
但是,我看到人们使用相同的方法并获得成功。
EDIT: I found a website where there was a solution.编辑:我找到了一个有解决方案的网站。 It was to compile not using
g++
.它是不使用
g++
编译的。 Rather use而是使用
g++-12 --sysroot=$(xcrun --show-sdk-path)
When I used this, it solved the problem.当我使用它时,它解决了这个问题。 Can someone explain why this happened?
有人可以解释为什么会这样吗?
The header error likely indicates the g++
you installed via Homebrew may not be compatible with the Apple Xcode version installed in the macos system directories. header 错误可能表示您通过 Homebrew 安装的
g++
可能与安装在 macos 系统目录中的 Apple Xcode 版本不兼容。
The solution is probably to reinstall one or both packages.解决方案可能是重新安装一个或两个软件包。
EDIT: g++-12 --sysroot=$(xcrun --show-sdk-path)
changes the search path for system header includes from the default (which was probably set when homebrew installed g++) to the one provided by the Xcode SDK currently installed.编辑:
g++-12 --sysroot=$(xcrun --show-sdk-path)
将系统 header 的搜索路径从默认值(可能是在自制程序安装 g++ 时设置的)更改为当前由 Xcode SDK 提供的搜索路径安装。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.