简体   繁体   English

如何确保我的默认 C/C++ 编译器是 GCC

[英]How do I make sure that my default C/C++ compiler is GCC

I'm trying to install Riak from source on macOS ( https://docs.riak.com/riak/kv/2.2.3/setup/installing/mac-osx.1.html#installing-from-source ).我正在尝试在 macOS 上从源代码安装 Riak( https://docs.riak.com/riak/kv/2.2.3/setup/installing/mac-osx.1.html#installing-from-source )。

There is a note:有一个注释:

Riak will not compile with Clang. Riak 不会与 Clang 一起编译。 Please make sure that your default C/C++ compiler is GCC请确保您的默认 C/C++ 编译器是 GCC

How do I find out which compiler is the default and how to change it?如何找出默认的编译器以及如何更改它?

macOS Catalina (10.15.4), which command prints: macOS Catalina (10.15.4),命令打印:

$ which clang
/usr/bin/clang
$ which gcc
/usr/bin/gcc

On macOS Catalina (and prior versions, and most likely subsequent versions too), there are two aspects to the problem and some suggested solutions.在 macOS Catalina(以及之前的版本,很可能还有后续版本)上,问题有两个方面和一些建议的解决方案。

What is the name of the compiler used by make by default? make默认使用的编译器名称是什么?

$ mkdir junk
$ cd junk
$ > x.cpp
$ > y.c
$ make x y
c++     x.cpp   -o x
cc     y.c   -o y
$ cd ..
$ rm -fr junk

This shows that the names used by make are cc and c++ .这表明make使用的名称是ccc++ Those are not obviously clang or clang++ , but neither are they obviously gcc and g++ .这些显然不是clangclang++ ,但也不是显然gccg++

$ which cc c++
/usr/bin/cc
/usr/bin/c++
$

Which compiler is it really?究竟是哪个编译器?

Which compiler really lives behind the names cc , c++ , gcc , g++ , clang , and clang++ ?哪个编译器真正存在于名称后面ccc++gccg++clangclang++ 754 FFZ 和 AE? We can check which compiler these really are by getting them to identify their version:我们可以通过让它们识别它们的版本来检查它们到底是哪个编译器:

$ for compiler in cc c++ gcc g++ clang clang++
> do
>     which $compiler
>     $compiler --version
> done
/usr/bin/cc
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
/usr/bin/c++
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
/usr/bin/gcc
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
/usr/bin/g++
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
/usr/bin/clang
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
/usr/bin/clang++
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
$

As you can see, the versions installed in /usr/bin are all the same compiler, and that compiler is clang or clang++ .可以看到, /usr/bin中安装的版本都是同一个编译器,编译器是clangclang++

This was run on a machine with macOS Mojave 10.14.6 and XCode 11.3.1.这是在装有 macOS Mojave 10.14.6 和 XCode 11.3.1 的机器上运行的。 The latest version of XCode — 11.4.1 — is only available on Catalina.最新版本的 XCode — 11.4.1 — 仅在 Catalina 上可用。 However, the general conclusion is the same — all the C and C++ compilers are really clang and clang++ in disguise.但是,一般结论是相同的——所有的 C 和 C++ 编译器实际上都是伪装的clangclang++

How do you get GNU GCC onto your machine?如何将 GNU GCC 安装到您的机器上?

How do you get a real GNU GCC — a real GCC, not clang in disguise — onto your machine?你如何得到一个真正的 GNU GCC——一个真正的 GCC,而不是clang伪装到你的机器上?

  • Use Brew to install GCC (I've not checked which version of GCC is current).使用Brew安装 GCC(我没有检查 GCC 的哪个版本是最新的)。
  • Or use MacPorts (again, I've not checked which version of GCC is current).或者使用MacPorts (同样,我没有检查 GCC 的哪个版本是最新的)。
  • If you're adventuresome, do it yourself (but I've not yet succeeded at building GCC 9.3.0 on Catalina; I have a GCC 9.2.0 built on macOS Mojave 10.14.x that works OK on Catalina though — with one environment variable needed to locate the headers).如果您喜欢冒险,请自己动手(但我还没有成功地在 Catalina 上构建 GCC 9.3.0;我有一个 GCC 9.2.0,在 Catalina 上构建在 macOS Mojave 10.14.x 上虽然可以工作定位标题所需的变量)。
  • Maybe Fink — it lists GCC 8.4 as being made available in 2020;也许是Fink——它列出了 GCC 8.4 将于 2020 年推出; I don't know about newer versions.我不知道新版本。

Be aware that Apple has taken to hiding the system header files miles out of the way (not in /usr/include — and you can't modify that part of the file system to add a symlink to where they've hidden them):请注意,Apple 已采取隐藏系统 header 文件的方式(不在/usr/include中 - 您无法修改文件系统的该部分以添加符号链接到他们隐藏它们的位置):

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include

(You mean you couldn't guess that? Me neither!) (你的意思是你猜不到?我也猜不到!)

How do you change the default compiler?如何更改默认编译器?

Once you have GCC installed somewhere appropriate, you need to ensure you use the 'real' GCC and not the 'fake' in /usr/bin .一旦你在适当的地方安装了 GCC ,你需要确保你使用“真实的” GCC 而不是/usr/bin中的“假”。 You do that in part by ensuring that the bin directory for the 'real' GCC occurs on your PATH before /usr/bin .您可以通过确保“真实” GCC 的 bin 目录出现在/usr/bin之前的路径上来部分地做到这一点。 I have GCC 9.3.0 installed under /opt/gcc/v9.3.0 , so /opt/gcc/v9.3.0/bin appears on my PATH long before /usr/bin does.我在 /opt/gcc/v9.3.0 下安装了/opt/gcc/v9.3.0 ,所以/opt/gcc/v9.3.0/bin早在/usr/bin出现在我的 PATH 上。

You also need to ensure that the configuration for riak (the software you're installing) uses the correct compilers.您还需要确保riak (您正在安装的软件)的配置使用正确的编译器。 If there's a ./configure script, run it with the correct path specified for the compilers.如果有./configure脚本,请使用为编译器指定的正确路径运行它。 For example, I might use:例如,我可能会使用:

./configure CC=/opt/gcc/v9.3.0/bin/gcc CXX=/opt/gcc/v9.3.0/bin/g++

You can also set these values as environment variables.您还可以将这些值设置为环境变量。

If it uses cmake or some other configuration package, you'll need to consult the installation instructions.如果它使用cmake或其他一些配置 package,则需要查阅安装说明。 That's usually README or sometimes INSTALL .这通常是README或有时是INSTALL


See also (increasingly older posts):另见(越来越老的帖子):

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使编译器在C ++中创建默认构造函数? - How can I make the compiler create the default constructors in C++? gcc确保没有编译c ++主题? - gcc Make sure that no c++ isms are compiled? 如何确保fopen()打开相对于可执行文件而不是C ++中当前目录的文件? - How do I make sure fopen() opens a file relative to the executable rather than my current directory in C++? 如何更改Atmel Studio 6中C ++的avr32-gcc C编译器而无需创建新项目? - How do I change the avr32-gcc C compiler for the C++ in Atmel Studio 6 without having to create a new project? 不可原谅的GCC C ++编译器 - Unforgiving GCC C++ compiler 最小的GCC C ++编译器 - Minimal GCC C++ Compiler 我怎么知道Ubuntu 11.04中gcc,g ++ / c ++的“默认包含目录”,“默认链接目录”和“默认链接库”? - How do I know the “default include directories”, “default link directories” and “default link libraries” of gcc, g++/c++ in Ubuntu 11.04? 如何在程序退出之前确保所有c ++ boost线程完成? - How can I make sure all of my c++ boost threads finish before the program exits? 我如何确保在我的 C++ 程序中所需的头文件安装在用户的机器上? - How would I make sure that the needed headers required in my C++ program are installed on the user's machine? 如何告诉超级计算机上的C ++编译器我的R包需要C ++ 0x? - How do I tell the C++ compiler on a supercomputer that my R package requires C++0x?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM