1

Our project is often cross compiled, so we want to allow a different target C++ compiler which is different from the one that runs on the frontend node. The CMakeLists.txt contain the following:

OPTION(target_cxx "select target CXX Compiler for building libqphix-codegen.a" ${CMAKE_CXX_COMPILER})
OPTION(target_cxxflags "select target CXXFLAGS for building libqphix-codege.a" ${CMAKE_CXX_FLAGS})

When I call that with cmake ~/Projekte/qphix-codegen/ -Disa=avx -DCMAKE_CXX_COMPILER=g++ -DCMAKE_CXX_FLAGS='-O3 -march=sandybridge', I would expect that target_cxx gets set to that g++ and the flags accordingly. However, the value of target_cxx is OFF.

I need to specify -Dtarget_cxx in order to get some other value than OFF.

Is there something one can do differently such that those variables get a sensible default value? I have the impression that the default value is evaluated too early.

Martin Ueding
  • 8,245
  • 6
  • 46
  • 92
  • 2
    The `OPTION()` call needs to be after the `project()` call (which evaluates `CMAKE_CXX_COMPILER`). But I'm not sure what you are trying to accomplish here. This looks more like something you put in different [toolchain files](https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html) that you ship with your project. – Florian Jun 16 '17 at 19:29
  • I have fixed this by using `set` instead of `option`. I realized that `option` is for boolean options only. Now it works just fine. The toolchain stuff looks interesting, I will take a look at that later. – Martin Ueding Jun 16 '17 at 20:17
  • Thanks, I didn't know about the `ON`/`OFF` restriction to `option()` command's initial value parameter. I've updated my answer for ["What's the CMake syntax to set and use variables?"](https://stackoverflow.com/questions/31037882/whats-the-cmake-syntax-to-set-and-use-variables) accordingly. Do you like to [self-answer](https://stackoverflow.com/help/self-answer) your question? – Florian Jun 17 '17 at 18:54

1 Answers1

0

The option() only works with boolean variables, it is just to let the user pass a number of flags. What is needed here is set(). There are two variants:

set(varname value)

and

set(varname [default] CACHE STRING helptext)

The former creates a variable that is inherited by all subdirectories, the latter creates a user servicable variable that has string type.

Martin Ueding
  • 8,245
  • 6
  • 46
  • 92