Both try_compile and check_cxx_source_compiles will compile and link a source file into an executable. If those operations succeed, then the output variable, omp_task_loop_test_1 for the former and omp_task_loop_test_2 for the latter, will be set to TRUE. The way this task is achieved is slightly different between the two commands, however. The check_<lang>_source_compiles family of commands is a simplified wrapper to the try_compile command. As such, it offers a minimal interface:
- The code snippet to be compiled has to be passed in as a CMake variable. Most of the time this means that files have to be read in using file(READ ...), as we have done in our example. The snippet is then saved to a file in the CMakeFiles/CMakeTmp subdirectory of the build directory.
- Fine-tuning compilation and linking has to be done by setting the following CMake variables before calling the function:
- CMAKE_REQUIRED_FLAGS to set the compiler flags
- CMAKE_REQUIRED_DEFINITIONS to set preprocessor macros
- CMAKE_REQUIRED_INCLUDES to set the list of include directories
- CMAKE_REQUIRED_LIBRARIES to set the list of libraries to link into the executable
- These variables have to be manually unset after calling the check_<lang>_compiles_function, to guarantee that further uses of the same variables does not have spurious contents.
This minimal interface reflects the fact that the test compilation is carried out by generating and executing build and link commands directly within the CMake invocation.
The command try_compile offers a more complete interface and two different modes of operation:
- The first one takes a full-fledged CMake project as input and will configure, build, and link it based on its CMakeLists.txt. This mode of operation offers more flexibility, since the project to be compiled can be arbitrarily complex.
- The second one, which we have used, where a source file is provided together with configuration options for include directories, link libraries, and compiler flags.
try_compile is thus based on invoking CMake on a project, either one where the CMakeLists.txt is already existing (in the first mode of operation) or one where the file is generated on the fly based on the arguments passed to the try_compile.