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

查看:3769
本文介绍了如何在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:

方法1:使用环境变量

对于C和C ++,设置 CC CXX 环境变量。不能保证此方法适用于所有发电机。 (特别是,如果您尝试设置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

方法2:使用cmake -D

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

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

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

将相应的 CMAKE_FOO_COMPILER 变量设置为有效的编译器名称或使用 set()的列表文件中的完整路径。这必须在设置任何语言之前完成(即:在任何 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")

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

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

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

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