feelfree
feelfree

Reputation: 11753

Unrecognized variable in .cmake file

I give the following example to illustrate my problem:

CMakeLists.txt

....
option(MY_TEST_VARIABLE "test" OFF)
include(my.cmake)
...

my.cmake

if(MY_TEST_VARIABLE)
  message("MY_TEST_VARIABLE is ON")
else()
  message("MY_TEST_VARIABLE is OFF")
end()

As you can see CMakeLists.txt will invoke my.cmake, and both files share the same variable MY_TEST_VARIABLE. In CMakeLists.txt, MY_TEST_VARIABLEis set,but it cannot be recognized in my.cmake. So when run cmake, the following message is given:

MY_TEST_VARIABLE is ON

So my question is how we can make the variable MY_TEST_VARIABLE visible for both files.

Upvotes: 0

Views: 188

Answers (1)

archz
archz

Reputation: 1072

MY_TEST_VARIABLE should be visible in both files and it probably is.

This problem can happen if you executed cmake the first time with option(MY_TEST_VARIABLE "test" ON) or manually modified CMakeCache.txt in your build directory.

If CMakeCache.txt already contain an option MY_TEST_VARIABLE with a value. It will override your call option(MY_TEST_VARIABLE "test" OFF) (which is an expected behavior as the option command provides a default value if the user did not select a value).

Check that your variable is not already set to ON in CMakeCache.txt (via cmake-gui or a text editor). If your project does not include other options (or if you want to only use the default values) you can even remove CMakeCache.txt to be sure to use the default value for your option.

Upvotes: 2

Related Questions