check_symbol_exists() and #define

Hello,

I try to detect a #define in a header file :

#define USAGE_QUALITY 0u

using

list(APPEND CMAKE_REQUIRED_INCLUDES ${TEST_INCLUDE_DIR})
check_symbol_exists(USAGE_QUALITY test/test.h usage_flag_exists)

but usage_flag_exists return false ?

thanks

Are you setting up the include directories correctly? Since you’re specifying the file to be included as test/test.h, it means that ${TEST_INCLUDE_DIR} must be a directory which contains directory test (which then contains test.h). Is that the case?

Yes, it’s that case.

Also note that the result of the check is cached, i.e. the symbol existence is only checked in the first CMake run. If you want to force re-evaluation, delete the usage_flag_exists cache variable (e.g. by passing -Uusage_flag_exists on CMake command line).

Is-it possible using the CMake GUI to delete cache variable ?

Yes. Select the variable and press “Remove Entry” button.

I don’t see cache variable usage_flag_exists in CMake GUI ?

You’re right, apparently the module sets the variable as INTERNAL. So you will have to run CMake with -U from the command line, or manually delete the variable from the CMakeCache.txt.

I added to the beginning of the project CMakeLists.txt file :

unset(usage_flag_exists CACHE)

is-it right ?

Yes, this will remove the cache variable at the start of each CMake run. Which means that the compilation check for the existence of USAGE_QUALITY will be performed during every CMake run too. It is up to you to decide if that is what you want; note that it visibly slows down the configure step.

Deleting internal variable in CMakeCache.txt doesn’t work…

It worked for me. Is there something else which might be interfering with your setup?

I remove in CMakeCache.txt

usage_flag_exists:INTERNAL=

it’s recreated when I click on Configure, without being updated

It’s not really “without being updated,” it’s just that the check fails to find the symbol. You can try running CMake with --debug-trycompile to force it to leave the buildsystem generated by the last try_compile() on disk. So, the steps would be:

  1. Ensure that check_symbol_exists(USAGE_QUALITY ...) is the only symbol check, try_compile(), or try_run() command which will run in your configure step (e.g. comment out all others, if any, or ensure they have their result already cached).
  2. Run cmake -Uusage_flag_exists --debug-trycompile . in your top-level binary directory.
  3. Look into CMakeFiles/CMakeScratch/TryCompile-* for the buildsystem CMake used to check the symbol’s existence, and manually inspect why it’s failing to find the symbol.

I also need to close the CMake GUI app.
Thanks for your help.