5

I am preparing an application which should work with and without GUI, so I use in my CMakeLists.txt the command

option (NEED_GUI "Include Qt support"  OFF) 

and

if (NEED_GUI)
  message("****GUI should be OFF****")
  add_subdirectory(QtGUI)   # The Qt-based graphics routines
endif (NEED_GUI)

Despite that I set the option OFF, I receive the message and the library is built. Where to look for an error?

Artemis
  • 2,553
  • 7
  • 21
  • 36
katang
  • 2,474
  • 5
  • 24
  • 48
  • 3
    Options are cached variables, so whatever you give is only an initial value (for more details see [here](http://stackoverflow.com/questions/31037882/whats-the-cmake-syntax-to-set-and-use-variables)). – Florian Mar 02 '16 at 10:37
  • 1
    @Florian: You should post that as an answer. – DevSolar Mar 02 '16 at 10:45

3 Answers3

17

Turning my comment into an answer

Your code looks good. So I'm assuming the problem here is that option() does transfer the value given into your CMakeCache.txt with the initial configuration step. After that you can only change it by modifying the cached entry for NEED_GUI. Changing the option in your CMakeLists.txt after you have generated your build environment will not update the cache anymore.

References

Community
  • 1
  • 1
Florian
  • 39,996
  • 9
  • 133
  • 149
  • I did not expect something similar caching, because I use the clean-all trick (http://stackoverflow.com/questions/9680420/looking-for-a-cmake-clean-command-to-clear-up-cmake-output), and I guessed it also cleaned cache. – katang Mar 02 '16 at 11:40
1
if (NEED_GUI MATCHES ON)

is the appropriate usage rather than

if (NEED_GUI)
Zebedee Mason
  • 21
  • 1
  • 1
1

You can try to do it without option. Try to write this
set (NEED_GUI OFF)
instead of
option (NEED_GUI "Include Qt support" OFF)

GolDen
  • 11
  • 4