如何让 CMake 在 MacOSX 上使用 GCC 而不是 Clang?

我找不到任何关于它的信息,但是只能找到相反的方法(例如,如何设置 CMake 以使用 clang)。

我已经使用 brew 安装了 gcc-4.8,安装了所有依赖项、头文件等,现在 CMake 拒绝使用 gcc。

我已经用别名和实际条目设置了 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'

然而,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
107271 次浏览

CMake doesn't (always) listen to CC and CXX. Instead use CMAKE_C_COMPILER and 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.

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.

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:

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.

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 -DCMAKE_Fortran_COMPILER=/usr/.../bin/gfortran-6.x.0

it should be enough to use CC and CXX environment variables, but in macOS are not "working as expected".

but it works in this way instead eg:

CXX="gcc-8" CC="gcc-8" cmake ..

I guess the case in OP was run on a macOS, and the real problem is /usr/bin/gcc and /usr/bin/g++ by default are clang and clang++.

% ls -al /usr/bin/clang
-rwxr-xr-x  1 root  wheel  167088  8 Dec 07:39 /usr/bin/clang
% ls -al /usr/bin/gcc
-rwxr-xr-x  1 root  wheel  167088  8 Dec 07:39 /usr/bin/gcc
% /usr/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 13.0.0 (clang-1300.0.29.30)
Target: x86_64-apple-darwin21.2.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

One way avoid that is to install gcc/g++ and then use the versioned binary names.

% which gcc-11 g++-11
/usr/local/bin/gcc-11
/usr/local/bin/g++-11
% gcc-11 --version
gcc-11 (Homebrew GCC 11.2.0_3) 11.2.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.

So, instead of

cmake .. -DCMAKE_C_COMPILER=/usr/bin/gcc -DCMAKE_CXX_COMPILER=/usr/bin/g++

one would use

cmake .. -DCMAKE_C_COMPILER=/usr/local/bin/gcc-11 -DCMAKE_CXX_COMPILER=/usr/local/bin/g++-11