转自:
https://wwyqianqian.github.io/2019/05/06/real-gcc/
今天编译一段带有 #include <bits/stdc++.h>
这样一个头文件的 c++ 代码时,遇到了报错
1 2 3 4 5 | fatal error: 'bits/stdc++.h' file not found #include <bits/stdc++.h> ^~~~~~~~~~~~~~~ 1 error generated. |
查了一下才知道:Mac OS X 10.9+ no longer uses GCC/libstdc++ but uses libc++ and Clang. 所以平时我们 macOS 使用的 gcc 实际上已经是 clang了。
1 2 3 4 5 6 7 | ~/work$ gcc -v Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1 Apple LLVM version 10.0.1 (clang-1001.0.46.4) Target: x86_64-apple-darwin18.5.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin |
如何解决呢?最方便的就是 brew install gcc
。在此之前请一定检查自己是否下载过 CLT ,我没有预先下载 CLT,导致自己电脑从源下载,并且自己跑编译,CPU 瞬间 100% 占用,烫的可以煎鸡蛋才发现异常(。
1 2 3 4 5 | ~/work$ brew config macOS: 10.14.4-x86_64 CLT: N/A Xcode: 10.2.1 |
CLT: N/A,于是要使用 xcode-select --install
命令来下载,苹果 CDN 很迷,请关闭全局梯子。
终于下载好啦,我们应该使用什么命令编译呢?显然不是 g++ main.cpp -o main,因为不管是 gcc 还是 g++ 这个名字已经被占用了。
1 2 3 4 | ~/work$ ls -l /usr/local/bin | grep g++ lrwxr-xr-x 1 qianqian admin 31 May 6 19:45 g++-8 -> ../Cellar/gcc/8.3.0_2/bin/g++-8 lrwxr-xr-x 1 qianqian admin 53 May 6 19:45 x86_64-apple-darwin18-g++-8 -> ../Cellar/gcc/8.3.0_2/bin/x86_64-apple-darwin18-g++-8 |
所以改成 g++-8 x.cpp -o x
即可啦w