如何为 CMake 指定新的 GCC 路径 [英] How to specify new GCC path for CMake

查看:51
本文介绍了如何为 CMake 指定新的 GCC 路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的操作系统是 centos,它在路径 /usr/bin/gcc 中有一个默认的 gcc.但是它很旧,我需要一个新版本的gcc.所以我在新路径 /usr/local/bin/gcc 中安装了一个新版本.

My OS is centos which has a default gcc in path /usr/bin/gcc. But it is old, I need a new version of gcc. So I install a new version in a new path /usr/local/bin/gcc.

但是当我运行 cmake 时,它仍然使用旧版本的 gcc path(/usr/bin/gcc) .如何指定 gcc 到新路径(/usr/local/bin/gcc).

But when I run cmake, it still uses the old version gcc path(/usr/bin/gcc) . How can I specify the gcc to new path(/usr/local/bin/gcc).

我试图用 /usr/local/bin/gcc 覆盖 /usr/bin/gcc,但它不起作用.

I have tried to overwrite /usr/bin/gcc with /usr/local/bin/gcc, but it not work.

推荐答案

不要覆盖CMAKE_C_COMPILER,而是导出CC(和CXX)在调用 cmake 之前:

Do not overwrite CMAKE_C_COMPILER, but export CC (and CXX) before calling cmake:

export CC=/usr/local/bin/gcc
export CXX=/usr/local/bin/g++
cmake /path/to/your/project
make

导出只需执行一次,即第一次配置项目时,将从 CMake 缓存中读取这些值.

The export only needs to be done once, the first time you configure the project, then those values will be read from the CMake cache.

UPDATE:关于为什么不在 Jake 的评论之后覆盖 CMAKE_C(XX)_COMPILER 的详细解释

UPDATE: longer explanation on why not overriding CMAKE_C(XX)_COMPILER after Jake's comment

我建议不要覆盖 CMAKE_C(XX)_COMPILER 值,主要有两个原因:因为它不能很好地与 CMake 的缓存配合使用,并且因为它破坏了编译器检查和工具检测.

I recommend against overriding the CMAKE_C(XX)_COMPILER value for two main reasons: because it won't play well with CMake's cache and because it breaks compiler checks and tooling detection.

使用 set 命令时,您有三个选项:

When using the set command, you have three options:

  • 没有缓存,创建一个普通变量
  • 使用缓存,创建一个缓存变量
  • 强制缓存,配置时总是强制缓存值

让我们看看对 set 的三种可能调用会发生什么:

Let's see what happens for the three possible calls to set:

无缓存

set(CMAKE_C_COMPILER /usr/bin/clang)
set(CMAKE_CXX_COMPILER /usr/bin/clang++)

执行此操作时,您会创建一个正常"的隐藏同名缓存变量的变量 CMAKE_C(XX)_COMPILER.这意味着您的编译器现在已硬编码在您的构建脚本中,您无法为其提供自定义值.如果您有多个具有不同编译器的构建环境,这将是一个问题.您可以在每次想要使用不同的编译器时更新您的脚本,但这首先消除了使用 CMake 的价值.

When doing this, you create a "normal" variable CMAKE_C(XX)_COMPILER that hides the cache variable of the same name. That means your compiler is now hard-coded in your build script and you cannot give it a custom value. This will be a problem if you have multiple build environments with different compilers. You could just update your script each time you want to use a different compiler, but that removes the value of using CMake in the first place.

好的,那么,让我们更新缓存...

Ok, then, let's update the cache...

带缓存

set(CMAKE_C_COMPILER /usr/bin/clang CACHE PATH "")
set(CMAKE_CXX_COMPILER /usr/bin/clang++ CACHE PATH "")

此版本将不起作用".CMAKE_C(XX)_COMPILER 变量已经在缓存中,所以除非你强制它,否则它不会更新.

This version will just "not work". The CMAKE_C(XX)_COMPILER variable is already in the cache, so it won't get updated unless you force it.

啊...让我们用力,然后...

Ah... let's use the force, then...

强制缓存

set(CMAKE_C_COMPILER /usr/bin/clang CACHE PATH "" FORCE)
set(CMAKE_CXX_COMPILER /usr/bin/clang++ CACHE PATH "" FORCE)

这与正常"几乎相同变量版本,唯一的区别是您的值将设置在缓存中,因此用户可以看到它.但是任何更改都会被 set 命令覆盖.

This is almost the same as the "normal" variable version, the only difference is your value will be set in the cache, so users can see it. But any change will be overwritten by the set command.

破坏编译器检查和工具

在配置过程的早期,CMake 对编译器进行检查:它工作吗?它能够生成可执行文件吗?等.它还使用编译器来检测相关工具,如arranlib.当您在脚本中覆盖编译器值时,为时已晚",所有检查和检测都已完成.

Early in the configuration process, CMake performs checks on the compiler: Does it work? Is it able to produce executables? etc. It also uses the compiler to detect related tools, like ar and ranlib. When you override the compiler value in a script, it's "too late", all checks and detections are already done.

例如,在我的机器上使用 gcc 作为默认编译器,当使用 set 命令到 /usr/bin/clang, ar设置为 /usr/bin/gcc-ar-7.在运行 CMake 之前使用导出时,它被设置为 /usr/lib/llvm-3.8/bin/llvm-ar.

For instance, on my machine with gcc as default compiler, when using the set command to /usr/bin/clang, ar is set to /usr/bin/gcc-ar-7. When using an export before running CMake it is set to /usr/lib/llvm-3.8/bin/llvm-ar.

这篇关于如何为 CMake 指定新的 GCC 路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆