This recipe will show how to use the check_<lang>_source_compiles function from the corresponding Check<LANG>SourceCompiles.cmake standard module, in order to assess whether the given compiler can compile a predefined code snippet into an executable. The command can help you ascertain whether:
- Your compiler supports desired features.
- The linker works properly and understands specific flags.
- Include directories and libraries found using find_package are usable.
In this recipe, we will show how to detect the task loop feature of the OpenMP 4.5 standard for its use in a C++ executable. We will use a sample C++ source file to probe whether the compiler supports such a feature. CMake offers an additional command, try_compile, to probe compilation. This recipe will show how to use both approaches.
You can use the CMake command-line interface to get documentation about specific modules (cmake --help-module <module-name>) and commands (cmake --help-command <command-name>). In our example, cmake --help-module CheckCXXSourceCompiles will output the documentation for the check_cxx_source_compiles function to screen, while cmake --help-command try_compile will do the same for the try_compile command.