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?
To select a specific compiler, you have several solutions, as explained in CMake wiki:
Method 1: use environment variables
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
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 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
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 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...
The reason this method should be avoided is because it hardcodes paths and compilers which is something that is usually configured in a package manager such as conan. Conan allows you to detect which compiler is preferable on a given platform and forwards this to cmake. You would make things harder for everyone if you were to hardcode it. That's why option 2 is the preferred way do do it.