Reputation: 831
I have the following variable in a .cmake file:
add_definitions(-DENABLE_TEST)
if(DEFINED ENABLE_TEST)
message("ENABLE_TEST defined")
else()
message("ENABLE_TEST NOT defined")
endif()
I have to use the variable in other CMakelists.txt too.
Why does the above "if" always coming out as not defined? How do I check if the variable is defined or not?
Upvotes: 1
Views: 2777
Reputation: 18243
The add_definitions()
command adds compile definitions to the compilation of your source files. It does not define CMake variables. The if(DEFINED ...
syntax checks for the existence of CMake or environment variables, not the existence of compile definitions.
You can use the set()
CMake command to define CMake variables, then subsequently, check for their existence in this manner:
set(ENABLE_TEST 1)
if(DEFINED ENABLE_TEST)
message("ENABLE_TEST defined")
else()
message("ENABLE_TEST NOT defined")
endif()
Upvotes: 4