cool_kuba
cool_kuba

Reputation: 21

CMake: Proper way to select compiler without hard coding its path

I am new to CMake and I am trying to create CMake for existing project which has to crosscompile ARM code on multiple Linux and Windows machines so hard coding path to compiler is not an option. So far path to compiler was added to each user path so you can just call armcc from console and correct compiler version would execute. I haven't found a way to specify compiler like that in CMake I tried:

  1. SET(CMAKE_C_COMPILER "armcc") - It returns: The CMAKE_C_COMPILER: armcc is not a full path and was not found in the PATH.
  2. find_program(CMAKE_C_COMPILER NAMES armcc) - it always returns /usr/bin/cc

I am sure that first message is not true because path to armcc is in PATH variable. I am using cmake version 3.6.2.

Upvotes: 1

Views: 651

Answers (1)

Cinder Biscuits
Cinder Biscuits

Reputation: 5259

This is spelled out in the CMake FAQ:

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 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 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")

project("YourProjectName")

My guess, since you haven't provided any code and didn't read the manual, is that you are trying to set the compiler after setting the language.

For your purpose, I would go with CMake's recommendation and use either Method 1 or Method 2 since you do not want to hardcode a path.

Upvotes: 1

Related Questions