try_compile.rst 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. try_compile
  2. -----------
  3. .. only:: html
  4. .. contents::
  5. Try building some code.
  6. Try Compiling Whole Projects
  7. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  8. .. code-block:: cmake
  9. try_compile(RESULT_VAR <bindir> <srcdir>
  10. <projectName> [<targetName>] [CMAKE_FLAGS <flags>...]
  11. [OUTPUT_VARIABLE <var>])
  12. Try building a project. The success or failure of the ``try_compile``,
  13. i.e. ``TRUE`` or ``FALSE`` respectively, is returned in ``RESULT_VAR``.
  14. In this form, ``<srcdir>`` should contain a complete CMake project with a
  15. ``CMakeLists.txt`` file and all sources. The ``<bindir>`` and ``<srcdir>``
  16. will not be deleted after this command is run. Specify ``<targetName>`` to
  17. build a specific target instead of the ``all`` or ``ALL_BUILD`` target. See
  18. below for the meaning of other options.
  19. Try Compiling Source Files
  20. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  21. .. code-block:: cmake
  22. try_compile(RESULT_VAR <bindir> <srcfile|SOURCES srcfile...>
  23. [CMAKE_FLAGS <flags>...]
  24. [COMPILE_DEFINITIONS <defs>...]
  25. [LINK_LIBRARIES <libs>...]
  26. [OUTPUT_VARIABLE <var>]
  27. [COPY_FILE <fileName> [COPY_FILE_ERROR <var>]]
  28. [<LANG>_STANDARD <std>]
  29. [<LANG>_STANDARD_REQUIRED <bool>]
  30. [<LANG>_EXTENSIONS <bool>]
  31. )
  32. Try building an executable from one or more source files. The success or
  33. failure of the ``try_compile``, i.e. ``TRUE`` or ``FALSE`` respectively, is
  34. returned in ``RESULT_VAR``.
  35. In this form the user need only supply one or more source files that include a
  36. definition for ``main``. CMake will create a ``CMakeLists.txt`` file to build
  37. the source(s) as an executable that looks something like this:
  38. .. code-block:: cmake
  39. add_definitions(<expanded COMPILE_DEFINITIONS from caller>)
  40. include_directories(${INCLUDE_DIRECTORIES})
  41. link_directories(${LINK_DIRECTORIES})
  42. add_executable(cmTryCompileExec <srcfile>...)
  43. target_link_libraries(cmTryCompileExec ${LINK_LIBRARIES})
  44. The options are:
  45. ``CMAKE_FLAGS <flags>...``
  46. Specify flags of the form ``-DVAR:TYPE=VALUE`` to be passed to
  47. the ``cmake`` command-line used to drive the test build.
  48. The above example shows how values for variables
  49. ``INCLUDE_DIRECTORIES``, ``LINK_DIRECTORIES``, and ``LINK_LIBRARIES``
  50. are used.
  51. ``COMPILE_DEFINITIONS <defs>...``
  52. Specify ``-Ddefinition`` arguments to pass to ``add_definitions``
  53. in the generated test project.
  54. ``COPY_FILE <fileName>``
  55. Copy the linked executable to the given ``<fileName>``.
  56. ``COPY_FILE_ERROR <var>``
  57. Use after ``COPY_FILE`` to capture into variable ``<var>`` any error
  58. message encountered while trying to copy the file.
  59. ``LINK_LIBRARIES <libs>...``
  60. Specify libraries to be linked in the generated project.
  61. The list of libraries may refer to system libraries and to
  62. :ref:`Imported Targets <Imported Targets>` from the calling project.
  63. If this option is specified, any ``-DLINK_LIBRARIES=...`` value
  64. given to the ``CMAKE_FLAGS`` option will be ignored.
  65. ``OUTPUT_VARIABLE <var>``
  66. Store the output from the build process the given variable.
  67. ``<LANG>_STANDARD <std>``
  68. Specify the :prop_tgt:`C_STANDARD`, :prop_tgt:`CXX_STANDARD`,
  69. or :prop_tgt:`CUDA_STANDARD` target property of the generated project.
  70. ``<LANG>_STANDARD_REQUIRED <bool>``
  71. Specify the :prop_tgt:`C_STANDARD_REQUIRED`,
  72. :prop_tgt:`CXX_STANDARD_REQUIRED`, or :prop_tgt:`CUDA_STANDARD_REQUIRED`
  73. target property of the generated project.
  74. ``<LANG>_EXTENSIONS <bool>``
  75. Specify the :prop_tgt:`C_EXTENSIONS`, :prop_tgt:`CXX_EXTENSIONS`,
  76. or :prop_tgt:`CUDA_EXTENSIONS` target property of the generated project.
  77. In this version all files in ``<bindir>/CMakeFiles/CMakeTmp`` will be
  78. cleaned automatically. For debugging, ``--debug-trycompile`` can be
  79. passed to ``cmake`` to avoid this clean. However, multiple sequential
  80. ``try_compile`` operations reuse this single output directory. If you use
  81. ``--debug-trycompile``, you can only debug one ``try_compile`` call at a time.
  82. The recommended procedure is to protect all ``try_compile`` calls in your
  83. project by ``if(NOT DEFINED RESULT_VAR)`` logic, configure with cmake
  84. all the way through once, then delete the cache entry associated with
  85. the try_compile call of interest, and then re-run cmake again with
  86. ``--debug-trycompile``.
  87. Other Behavior Settings
  88. ^^^^^^^^^^^^^^^^^^^^^^^
  89. If set, the following variables are passed in to the generated
  90. try_compile CMakeLists.txt to initialize compile target properties with
  91. default values:
  92. * :variable:`CMAKE_ENABLE_EXPORTS`
  93. * :variable:`CMAKE_LINK_SEARCH_START_STATIC`
  94. * :variable:`CMAKE_LINK_SEARCH_END_STATIC`
  95. * :variable:`CMAKE_POSITION_INDEPENDENT_CODE`
  96. If :policy:`CMP0056` is set to ``NEW``, then
  97. :variable:`CMAKE_EXE_LINKER_FLAGS` is passed in as well.
  98. The current settings of :policy:`CMP0065` and :policy:`CMP0083` are set in the
  99. generated project.
  100. Set the :variable:`CMAKE_TRY_COMPILE_CONFIGURATION` variable to choose
  101. a build configuration.
  102. Set the :variable:`CMAKE_TRY_COMPILE_TARGET_TYPE` variable to specify
  103. the type of target used for the source file signature.
  104. Set the :variable:`CMAKE_TRY_COMPILE_PLATFORM_VARIABLES` variable to specify
  105. variables that must be propagated into the test project. This variable is
  106. meant for use only in toolchain files.
  107. If :policy:`CMP0067` is set to ``NEW``, or any of the ``<LANG>_STANDARD``,
  108. ``<LANG>_STANDARD_REQUIRED``, or ``<LANG>_EXTENSIONS`` options are used,
  109. then the language standard variables are honored:
  110. * :variable:`CMAKE_C_STANDARD`
  111. * :variable:`CMAKE_C_STANDARD_REQUIRED`
  112. * :variable:`CMAKE_C_EXTENSIONS`
  113. * :variable:`CMAKE_CXX_STANDARD`
  114. * :variable:`CMAKE_CXX_STANDARD_REQUIRED`
  115. * :variable:`CMAKE_CXX_EXTENSIONS`
  116. * :variable:`CMAKE_CUDA_STANDARD`
  117. * :variable:`CMAKE_CUDA_STANDARD_REQUIRED`
  118. * :variable:`CMAKE_CUDA_EXTENSIONS`
  119. Their values are used to set the corresponding target properties in
  120. the generated project (unless overridden by an explicit option).