Checks of the type outlined in this recipe are not always bulletproof and can generate both false positives and false negatives. As an example, you can try to comment out the lines containing CMAKE_REQUIRED_LIBRARIES and the example will still report "Success". The reason for this is that OpenMP pragmas will then be ignored by the compiler.
What should you do when you suspect that a wrong result is being returned? The CMakeOutput.log and CMakeError.log files in the CMakeFiles subdirectory of the build directory offer clues as to what went wrong. They report the standard output and standard error for operations run by CMake. If you suspect false positives, you should check the former, by searching for the variable set to hold the result of the compilation check. If you suspect false negatives, you should check the latter.
Debugging try_compile will require some care. CMake erases all files generated by that command, even if the check was unsuccessful. Fortunately, --debug-trycompile will prevent CMake from cleaning up. If there are multiple calls to try_compile in your code, you will only be able to debug them one at a time:
- Run CMake once, without --debug-trycompile. All try_compile commands will be run and their execution directories and files cleaned up.
- Erase the variable holding the result of the check from the CMake cache. The cache is saved into the CMakeCache.txt file. To clear the contents of a variable, you can use the -U CLI switch followed by the name of the variable, which will be interpreted as a globbing expression and may thus use * and ?:
$ cmake -U <variable-name>
- Run CMake once more, with --debug-trycompile. Only the check for which the cache was cleared up will be rerun. Its execution directories and files will not be cleaned up this time.