如何在 CMake 中指定编译器? [英] How to specify a compiler in CMake?

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

问题描述

我想使用 IAR 编译器.我注意到 CMake 已经有一堆关于这个编译器的文件:

I would like to use the IAR compiler. I noticed CMake has already have a bunch of files about this compiler:

https://github.com/jevinskie/cmake/blob/master/Modules/Compiler/IAR.cmake

从我读到的常见解决方案是在我的 CMakeLists.txt 中手动指定所有工具链:

From what I read the common solution is to specify manually ALL the toolchain in my CMakeLists.txt:

set(CMAKE_C_COMPILER iccarm)
set(CMAKE_CPP_COMPILER iccarm)

CMake 如何将这些定义与Modules/Compiler/IAR.cmake"链接起来?

How CMake can link these definitions with `Modules/Compiler/IAR.cmake"?

我以为我只需要这样做

include("Modules/Compiler/IAR.cmake")

指定 IAR 编译器的正确方法是什么?

What is the correct way to specify my IAR compiler?

当我这样做

cmake .

它仍然尝试使用 gcc 而不是我的 IAR 编译器.为什么?

It still tries to use gcc instead of my IAR compiler. Why?

推荐答案

要选择特定的编译器,您有多种解决方案,如 CMake wiki:

To select a specific compiler, you have several solutions, as exaplained in CMake wiki:

方法一:使用环境变量

对于 C 和 C++,设置 CCCXX 环境变量.此方法不保证适用于所有生成器.(具体来说,如果您尝试设置 Xcode 的 GCC_VERSION,此方法会混淆 Xcode.)例如:

For C and C++, set the CC and CXX environment variables. This method is not guaranteed to work for all generators. (Specifically, if you are trying to set Xcode's GCC_VERSION, this method confuses Xcode.) For example:

CC=gcc-4.2 CXX=/usr/bin/g++-4.2 cmake -G "Your Generator" path/to/your/source

方法二:使用cmake -D

使用 cmake -D 在命令行上将适当的 CMAKE_FOO_COMPILER 变量设置为有效的编译器名称或完整路径.例如:

Set the appropriate CMAKE_FOO_COMPILER variable(s) to a valid compiler name or full path on the command-line using cmake -D. For example:

cmake -G "Your Generator" -D CMAKE_C_COMPILER=gcc-4.2 -D CMAKE_CXX_COMPILER=g++-4.2 path/to/your/source

方法三(避免):使用set()

使用 set() 将适当的 CMAKE_FOO_COMPILER 变量设置为列表文件中的有效编译器名称或完整路径.这必须在设置任何语言之前完成(即:在任何 project()enable_language() 命令之前).例如:

Set the appropriate CMAKE_FOO_COMPILER variable(s) to a valid compiler name or full path in a list file using set(). This must be done before any language is set (ie: before any project() or enable_language() command). For example:

set(CMAKE_C_COMPILER "gcc-4.2")
set(CMAKE_CXX_COMPILER "/usr/bin/g++-4.2")

project("YourProjectName")

维基没有提供应避免使用第三种方法的原因......

The wiki doesn't provide reason why 3rd method should be avoided...

这篇关于如何在 CMake 中指定编译器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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