我希望 MacOS 中的 Clion 使用 g++-11 而不是 Clang 进行编译。当我设置工具链时,它没有通过测试。 Clion 给出“测试 Cmake 运行已完成,但出现错误”。这是屏幕截图
我可以点击“更多”来检查错误。
-- The C compiler identification is unknown
-- The CXX compiler identification is unknown
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Check for working C compiler: /usr/local/bin/gcc-11
-- Check for working C compiler: /usr/local/bin/gcc-11 - broken
CMake Error at /Applications/CLion.app/Contents/bin/cmake/mac/x64/share/cmake-3.27/Modules/CMakeTestCCompiler.cmake:67 (message):
The C compiler
"/usr/local/bin/gcc-11"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: '/private/var/folders/lf/x7t4ln495yx9rlng8jr8yk580000gn/T/cmake_check_environment1/_build7996487480848918631/CMakeFiles/CMakeScratch/TryCompile-Fl9cKx'
Run Build Command(s): /Applications/CLion.app/Contents/bin/ninja/mac/x64/ninja -v cmTC_8f62f
[1/2] /usr/local/bin/gcc-11 -o CMakeFiles/cmTC_8f62f.dir/testCCompiler.c.o -c /private/var/folders/lf/x7t4ln495yx9rlng8jr8yk580000gn/T/cmake_check_environment1/_build7996487480848918631/CMakeFiles/CMakeScratch/TryCompile-Fl9cKx/testCCompiler.c
FAILED: CMakeFiles/cmTC_8f62f.dir/testCCompiler.c.o
/usr/local/bin/gcc-11 -o CMakeFiles/cmTC_8f62f.dir/testCCompiler.c.o -c /private/var/folders/lf/x7t4ln495yx9rlng8jr8yk580000gn/T/cmake_check_environment1/_build7996487480848918631/CMakeFiles/CMakeScratch/TryCompile-Fl9cKx/testCCompiler.c
clang (LLVM option parsing): Unknown command line argument '-x86-pad-for-align=false'. Try: 'clang (LLVM option parsing) --help'
clang (LLVM option parsing): Did you mean '--x86-slh-loads=false'?
ninja: build stopped: subcommand failed.
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:2 (project)
-- Configuring incomplete, errors occurred!
Error code: 1
看起来测试运行时使用了一些 clang 参数?
ChatGPT 提示可能是环境变量路径的问题,但在我看来没问题。以下是有关 PATH 的命令行输出:
(base) myname@mynames-MacBook-Pro bin % which gcc
/usr/bin/gcc
(base) myname@mynames-MacBook-Pro bin % which gcc-11
/usr/local/bin/gcc-11
(base) myname@mynames-MacBook-Pro bin % which gcc-14
/usr/local/bin/gcc-14
(base) myname@mynames-MacBook-Pro bin % echo $PATH
/Users/myname/anaconda3/bin:/Users/myname/anaconda3/condabin:/Library/Frameworks/Python.framework/Versions/3.12/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/myname/Library/Python/3.9/bin:/Library/Frameworks/Python.framework/Versions/3.12/bin/python3.12:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/opt/X11/bin:/Library/Apple/usr/bin
这是我的 mac 上的 gcc 版本信息。一些答案表明 gcc-11 只是 clang 的别名。但根据版本信息,看起来gcc-11和gcc-14肯定是homebrew安装的版本?抱歉,我是一个 C++ 初学者,对此并不能 100% 确定。
(base) myname@myname-MacBook-Pro bin % gcc --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.0 (clang-1200.0.32.21)
Target: x86_64-apple-darwin23.4.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
(base) myname@myname-MacBook-Pro bin % gcc-11 --version
gcc-11 (Homebrew GCC 11.4.0) 11.4.0
Copyright (C) 2021 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.
(base) myname@myname-MacBook-Pro bin % gcc-14 --version
gcc-14 (Homebrew GCC 14.2.0) 14.2.0
Copyright (C) 2024 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.
答案建议卸载 gcc-11、brew 删除 gcc-11 并使用 gcc-14。我
brew remove gcc@11
并在Clion设置gcc-14。这是新的屏幕截图。]2错误日志基本相同:
-- The C compiler identification is unknown
-- The CXX compiler identification is unknown
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Check for working C compiler: /usr/local/bin/gcc-14
-- Check for working C compiler: /usr/local/bin/gcc-14 - broken
CMake Error at /Applications/CLion.app/Contents/bin/cmake/mac/x64/share/cmake-3.27/Modules/CMakeTestCCompiler.cmake:67 (message):
The C compiler
"/usr/local/bin/gcc-14"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: '/private/var/folders/lf/x7t4ln495yx9rlng8jr8yk580000gn/T/cmake_check_environment/_build4029476531814106616/CMakeFiles/CMakeScratch/TryCompile-1D8OsD'
Run Build Command(s): /Applications/CLion.app/Contents/bin/ninja/mac/x64/ninja -v cmTC_77a76
[1/2] /usr/local/bin/gcc-14 -o CMakeFiles/cmTC_77a76.dir/testCCompiler.c.o -c /private/var/folders/lf/x7t4ln495yx9rlng8jr8yk580000gn/T/cmake_check_environment/_build4029476531814106616/CMakeFiles/CMakeScratch/TryCompile-1D8OsD/testCCompiler.c
FAILED: CMakeFiles/cmTC_77a76.dir/testCCompiler.c.o
/usr/local/bin/gcc-14 -o CMakeFiles/cmTC_77a76.dir/testCCompiler.c.o -c /private/var/folders/lf/x7t4ln495yx9rlng8jr8yk580000gn/T/cmake_check_environment/_build4029476531814106616/CMakeFiles/CMakeScratch/TryCompile-1D8OsD/testCCompiler.c
clang (LLVM option parsing): Unknown command line argument '-x86-pad-for-align=false'. Try: 'clang (LLVM option parsing) --help'
clang (LLVM option parsing): Did you mean '--x86-slh-loads=false'?
ninja: build stopped: subcommand failed.
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:2 (project)
-- Configuring incomplete, errors occurred!
Error code: 1
不要使用 gcc-11,这是 Apple clang 12 的别名。卸载 gcc-11
brew remove gcc-11
并使用 gcc-14,尽管如果可以使用 Apple clang 就没有多大意义。我敢打赌,您在 clang 中默认不受支持的 C++11 或更高版本遇到问题,这就是您想要使用 GCC 的原因,那么您应该在 CLion 中显式设置更新的 C++ 标准。