如何在Mac上查找GCC版本

60

我正在使用Mac机器上的OS 10.9。我想知道我正在使用的gcc版本。因此,我在终端上尝试了gcc --version,并得到以下结果:

$ gcc --version
Configured with: --prefix=/Applications/Xcode5-DP.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode5-DP.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/c++/4.2.1
Apple LLVM version 5.0 (clang-500.1.58) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.0.0
Thread model: posix

在输出中没有与gcc相关的详细信息,但有clang。我不确定gcc命令是否执行clang还是gcc(gnu)。


2
4.2.1,它在你的问题中的 gcc --version 输出中。编辑:请参见第一行输出的文件路径末尾(以 Configured with... 开头)。 - admdrew
@admdrew 但输出中没有指定 gcc 术语。 - subhash kumar singh
@subh.singh - 不太确定您在说什么,但是如果您通过Xcode安装,则4.2.1是标准版本,并且输出中引用的正是该版本。 - admdrew
1
@admdrew 输出显示这甚至不是gcc,而是clang的3.3快照版本。 - nos
@admdrew:那是头文件的版本号,而不是编译器可执行文件的版本号。 - Eric Postpischil
显示剩余2条评论
6个回答

36

看起来您的路径上实际上没有gcc。最近的Xcode版本安装了一个名为“gcc”的链接,实际上是指向Clang。


我可以假设我正在运行gcc 4.2.1,并且clang将遵循gcc 4.2.1的所有标准。 - subhash kumar singh
我不知道Clang和gcc有多相似。但是我知道,它们的目标是源代码兼容,包括许多(全部?)gcc扩展。 - danfuzz
3
@danfuzz并非所有的GCC扩展都被支持,请参考有意不支持的GCC扩展尚未实现的GCC扩展 - tab

26
gcc -dumpversion | cut -f1 -d.

-dumpversion 打印编译器版本(例如,3.0)- 仅打印版本信息,不进行其他操作。

以下编译器/别名同样适用:

cc -dumpversion
g++ -dumpversion
clang -dumpversion
tcc -dumpversion

自动解析 GCC 输出时要小心:

  • --version 输出可能会被本地化(例如俄语、中文等)
  • GCC 可能使用选项 --with-gcc-major-version-only 进行构建。而一些发行版(例如Fedora)已经在使用该选项。
  • GCC 可能使用选项 --with-pkgversion 进行构建。而 --version 的输出将包含类似于 Android (基于 r346389c 的 5220042) clang version 8.0.7 的内容(这是真实的版本字符串)。

2
我有4.2.1,但只显示了4。这也是一种进步!;) - gsamaras
6
@gsamaras 请随意尝试使用“-f1”,“-f1,2”和“-f1,2,3” :) - serghei

19

苹果提供的工具已经从GCC切换到Clang。gcc命令被链接到clang作为一种便利。在OS X 10.9中,除非您独立安装了GCC,否则您的系统上没有GCC。


4

如果您通过brew install安装了gcc,可能已经安装为gcc-11

您可以运行brew info gcc来获取其安装路径,并通过列出目录来获取二进制文件的确切名称。

$ brew info gcc
gcc: stable 11.2.0 (bottled), HEAD
GNU compiler collection
https://gcc.gnu.org/
/usr/local/Cellar/gcc/11.2.0_3 (2,163 files, 459.8MB) *
...

$ ls /usr/local/Cellar/gcc/11.2.0_3/bin
c++-11                  gcc-ar-11               gcov-dump-11                gfortran                x86_64-apple-darwin21-g++-11        x86_64-apple-darwin21-gcc-ranlib-11
cpp-11                  gcc-nm-11               gcov-tool-11                gfortran-11             x86_64-apple-darwin21-gcc-11        x86_64-apple-darwin21-gcc-tmp
g++-11                  gcc-ranlib-11               gdc                 lto-dump-11             x86_64-apple-darwin21-gcc-ar-11     x86_64-apple-darwin21-gdc-11
gcc-11                  gcov-11                 gdc-11                  x86_64-apple-darwin21-c++-11        x86_64-apple-darwin21-gcc-nm-11     x86_64-apple-darwin21-gfortran-11

使用gcc-11 -v会显示您安装的gcc实际版本。

$ gcc-11 -v
Using built-in specs.
COLLECT_GCC=gcc-11
COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc/11.2.0_3/bin/../libexec/gcc/x86_64-apple-darwin21/11/lto-wrapper
Target: x86_64-apple-darwin21
Configured with: ../configure --prefix=/usr/local/opt/gcc --libdir=/usr/local/opt/gcc/lib/gcc/11 --disable-nls --enable-checking=release --with-gcc-major-version-only --enable-languages=c,c++,objc,obj-c++,fortran,d --program-suffix=-11 --with-gmp=/usr/local/opt/gmp --with-mpfr=/usr/local/opt/mpfr --with-mpc=/usr/local/opt/libmpc --with-isl=/usr/local/opt/isl --with-zstd=/usr/local/opt/zstd --with-pkgversion='Homebrew GCC 11.2.0_3' --with-bugurl=https://github.com/Homebrew/homebrew-core/issues --enable-libphobos --build=x86_64-apple-darwin21 --with-system-zlib --disable-multilib --with-native-system-header-dir=/usr/include --with-sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.2.0 (Homebrew GCC 11.2.0_3) 

2
gcc -dumpversion | cut -f1 -f2 -f3 -d.

0

gcc --version 你可以找到你的gcc路径。

sudo ln -s /Library/Developer/CommandLineTools/usr/bin/gcc /usr/local/bin/gcc


你的回答可以通过提供更多支持信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心中找到有关如何编写良好答案的更多信息。 - Community

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