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
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)
How CMake can link these definitions with `Modules/Compiler/IAR.cmake"?
I thought I would just have to do
include("Modules/Compiler/IAR.cmake")
What is the correct way to specify my IAR compiler?
When I do
cmake .
It still tries to use gcc
instead of my IAR compiler. Why?
If you don't want to use your PC's standard compiler, you have to give CMake the path to the compiler. You do this via environment variables, a toolchain file or direct definitions in the CMake command line (see e.g. CMake Error at CMakeLists.txt:30 (project): No CMAKE_C_COMPILER could be found).
Putting the compiler's name/path into your CMakeLists.txt would stop your project from being cross-platform.
CMake does check for the compiler ids by compiling special C/C++ files. So no need to manually include from
Module/Compiler
orModule/Platform
.This will be automatically done by CMake based on its compiler and platform checks.
References
You can call
cmake
like this:or
You need to create a toolchain file, and use the CmakeForceCompiler module.
Here is an example toolchain file for bare-metal ARM development with IAR:
The last line is necessary because CMake will try to compile a test program with the compiler to make sure it works and to get some version information from preprocessor defines. Without this line, CMake will use add_executable() for the test program, and you will get the error "The C compiler "XXX" is not able to compile a simple test program." This is because the test program fails to link, as it doesn't have your custom linker file (I'm assuming bare-metal development since this is what IAR is usually used for). This line tells CMake to use add_library() instead, which makes the test succeed without the linker file. Source of this workaround: this CMake mailing list post.
Then, assuming that your toolchain file is named iar-toolchain.cmake, invoke CMake like this:
To select a specific compiler, you have several solutions, as exaplained in CMake wiki:
Method 1: use environment variables
For C and C++, set the
CC
andCXX
environment variables. This method is not guaranteed to work for all generators. (Specifically, if you are trying to set Xcode'sGCC_VERSION
, this method confuses Xcode.) For example:Method 2: use cmake -D
Set the appropriate
CMAKE_FOO_COMPILER
variable(s) to a valid compiler name or full path on the command-line usingcmake -D
. For example:Method 3 (avoid): use set()
Set the appropriate
CMAKE_FOO_COMPILER
variable(s) to a valid compiler name or full path in a list file usingset()
. This must be done before any language is set (ie: before anyproject()
orenable_language()
command). For example:The wiki doesn't provide reason why 3rd method should be avoided...