C++ 如何让 CMake 在 Mac OS X 上使用 GCC 而不是 Clang?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24380456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I make CMake use GCC instead of Clang on Mac OS X?
提问by ?lex
I can't find any info on it, but only the other way around (e.g., how to set CMake to use clang).
我找不到任何关于它的信息,但只能反过来(例如,如何设置 CMake 以使用 clang)。
I've installed gcc-4.8 using brew, setup all dependencies, headers, etc, and now CMake refuses to use gcc.
我已经使用 brew 安装了 gcc-4.8,设置了所有依赖项、头文件等,现在 CMake 拒绝使用 gcc。
I've set my bash profile with both aliases and actual entries:
我已经使用别名和实际条目设置了我的 bash 配置文件:
export CC=/usr/bin/gcc
export CXX=/usr/bin/g++
alias gcc='gcc-4.8'
alias cc='gcc-4.8'
alias g++='g++-4.8'
alias c++='c++-4.8'
Yet CMake stubbornly refuses to use gcc and instead reverts back to clang:
然而,CMake 顽固地拒绝使用 gcc,而是恢复为 clang:
air:build alex$ cmake -DCMAKE_BUILD_TYPE=DEBUG ..
-- The C compiler identification is Clang 5.1.0
-- The CXX compiler identification is Clang 5.1.0
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
回答by rubenvb
CMake doesn't (always) listen to CC
and CXX
. Instead use CMAKE_C_COMPILER
and CMAKE_CXX_COMPILER
:
CMake 不(总是)听CC
和CXX
。而是使用CMAKE_C_COMPILER
和CMAKE_CXX_COMPILER
:
cmake -DCMAKE_C_COMPILER=/usr/bin/gcc -DCMAKE_CXX_COMPILER=/usr/bin/g++ ...
See also the documentation.
另请参阅文档。
Alternatively, you can provide a toolchain file, but that might be overkill in this case.
或者,您可以提供一个工具链文件,但在这种情况下这可能有点过头了。
回答by Norman B. Lancaster
Current versions of CMake do not respect the CC and CXX environment variables like one would expect. Specifically if they are absolute paths to the compiler binaries they seem to be ignored. On my system with a freshly compiled cmake 3.7.1 I have to do cmake -H. -Bbuild -DCMAKE_C_COMPILER=$CC -DCMAKE_CXX_COMPILER=$CXX
.
当前版本的 CMake 不像人们期望的那样尊重 CC 和 CXX 环境变量。特别是如果它们是编译器二进制文件的绝对路径,它们似乎被忽略了。在我使用新编译的 cmake 3.7.1 的系统上,我必须执行cmake -H. -Bbuild -DCMAKE_C_COMPILER=$CC -DCMAKE_CXX_COMPILER=$CXX
.
As others have stated it is not a great idea to force a compiler choice within your CMakeLists.txt, however if this is required for your use case here's how you do it:
正如其他人所说,在您的 CMakeLists.txt 中强制选择编译器并不是一个好主意,但是如果您的用例需要这样做,您可以这样做:
cmake_minimum_required(VERSION 3.5) # Or whatever version you use
# THIS HAS TO COME BEFORE THE PROJECT LINE
set(CMAKE_C_COMPILER "gcc")
set(CMAKE_CXX_COMPILER "g++")
# THIS HAS TO COME BEFORE THE PROJECT LINE
project(my_project VERSION 0.0.0 LANGUAGES C CXX)
In this case cmake will fail if the indicated compiler is not found. Note that you must set these variables before the project line as this command is what finds and configures the compilers.
在这种情况下,如果未找到指定的编译器,cmake 将失败。请注意,您必须在项目行之前设置这些变量,因为此命令用于查找和配置编译器。
回答by Norman B. Lancaster
Just to add that there is also a CMake variable "CMAKE_Fortran_COMPILER" to pick up GNU Fortran rather than clang fortran. But it seems to be missing in the documentation
只是补充一点,还有一个 CMake 变量“CMAKE_Fortran_COMPILER”来获取 GNU Fortran 而不是 clang fortran。但它似乎在文档中丢失
cmake -DCMAKE_Fortran_COMPILER=/usr/.../bin/gfortran-6.x.0
cmake -DCMAKE_Fortran_COMPILER=/usr/.../bin/gfortran-6.x.0
回答by Raffaello
it should be enough to use CC
and CXX
environment variables, but in macOS are not "working as expected".
它应该是足够使用CC
和CXX
环境变量,但在MacOS上不是“按预期工作”。
but it works in this way instead eg:
但它以这种方式工作,例如:
CXX="gcc-8" CC="gcc-8" cmake ..