CheckCXXSourceCompiles.cmake 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. CheckCXXSourceCompiles
  5. ----------------------
  6. This module provides a command to check whether a C++ source can be built.
  7. Load this module in a CMake project with:
  8. .. code-block:: cmake
  9. include(CheckCXXSourceCompiles)
  10. Commands
  11. ^^^^^^^^
  12. This module provides the following command:
  13. .. command:: check_cxx_source_compiles
  14. Checks once whether the given C++ source code can be built:
  15. .. code-block:: cmake
  16. check_cxx_source_compiles(<code> <variable> [FAIL_REGEX <regexes>...])
  17. This command checks once that the source supplied in ``<code>`` can be
  18. compiled (and linked into an executable). The result of the check is
  19. stored in the internal cache variable specified by ``<variable>``.
  20. The arguments are:
  21. ``<code>``
  22. C++ source code to check. This must be an entire program, as written
  23. in a file containing the body block. All symbols used in the source code
  24. are expected to be declared as usual in their corresponding headers.
  25. ``<variable>``
  26. Variable name of an internal cache variable to store the result of the
  27. check, with boolean true for success and boolean false for failure.
  28. ``FAIL_REGEX <regexes>...``
  29. If one or more regular expression patterns are provided, then failure is
  30. determined by checking if anything in the compiler output matches any of
  31. the specified regular expressions.
  32. .. rubric:: Variables Affecting the Check
  33. The following variables may be set before calling this command to modify
  34. the way the check is run:
  35. .. include:: /module/include/CMAKE_REQUIRED_FLAGS.rst
  36. .. include:: /module/include/CMAKE_REQUIRED_DEFINITIONS.rst
  37. .. include:: /module/include/CMAKE_REQUIRED_INCLUDES.rst
  38. .. include:: /module/include/CMAKE_REQUIRED_LINK_OPTIONS.rst
  39. .. include:: /module/include/CMAKE_REQUIRED_LIBRARIES.rst
  40. .. include:: /module/include/CMAKE_REQUIRED_LINK_DIRECTORIES.rst
  41. .. include:: /module/include/CMAKE_REQUIRED_QUIET.rst
  42. .. include:: /module/include/CMAKE_TRY_COMPILE_TARGET_TYPE.rst
  43. Examples
  44. ^^^^^^^^
  45. The following example demonstrates how to check whether the C++ compiler
  46. supports a specific language feature. In this case, the check verifies if
  47. the compiler supports ``C++11`` lambda expressions. The result is stored
  48. in the internal cache variable ``HAVE_CXX11_LAMBDAS``:
  49. .. code-block:: cmake
  50. include(CheckCXXSourceCompiles)
  51. check_cxx_source_compiles("
  52. int main()
  53. {
  54. auto lambda = []() { return 42; };
  55. return lambda();
  56. }
  57. " HAVE_CXX11_LAMBDAS)
  58. See Also
  59. ^^^^^^^^
  60. * The :module:`CheckSourceCompiles` module for a more general command to
  61. check whether source can be built.
  62. * The :module:`CheckSourceRuns` module to check whether source can be built
  63. and run.
  64. #]=======================================================================]
  65. include_guard(GLOBAL)
  66. include(Internal/CheckSourceCompiles)
  67. macro(CHECK_CXX_SOURCE_COMPILES SOURCE VAR)
  68. cmake_check_source_compiles(CXX "${SOURCE}" ${VAR} ${ARGN})
  69. endmacro()