如何让 CMake 在 Mac OS X 上使用 GCC 而不是 Clang?

2023-08-27C/C++开发问题
44

本文介绍了如何让 CMake 在 Mac OS X 上使用 GCC 而不是 Clang?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我找不到有关它的任何信息,只能找到相反的信息(例如,如何将 CMake 设置为使用 clang).

I can't find any info on it, but only the other way around (e.g., how to set CMake to use clang).

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

I've installed gcc-4.8 using brew, setup all dependencies, headers, etc, and now CMake refuses to use gcc.

我已经使用别名和实际条目设置了我的 bash 配置文件:

I've set my bash profile with both aliases and actual entries:

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:

Yet CMake stubbornly refuses to use gcc and instead reverts back to 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

推荐答案

CMake 不(总是)听 CCCXX.而是使用 CMAKE_C_COMPILERCMAKE_CXX_COMPILER:

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++ ...

另见文档.

或者,您可以提供一个工具链文件,但在这种情况下这可能有点矫枉过正.

Alternatively, you can provide a toolchain file, but that might be overkill in this case.

这篇关于如何让 CMake 在 Mac OS X 上使用 GCC 而不是 Clang?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6