try_compile.rst 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. try_compile
  2. -----------
  3. .. only:: html
  4. .. contents::
  5. Try building some code.
  6. .. _`Try Compiling Whole Projects`:
  7. Try Compiling Whole Projects
  8. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  9. .. code-block:: cmake
  10. try_compile(<resultVar> <bindir> <srcdir>
  11. <projectName> [<targetName>] [CMAKE_FLAGS <flags>...]
  12. [OUTPUT_VARIABLE <var>])
  13. Try building a project. The success or failure of the ``try_compile``,
  14. i.e. ``TRUE`` or ``FALSE`` respectively, is returned in ``<resultVar>``.
  15. In this form, ``<srcdir>`` should contain a complete CMake project with a
  16. ``CMakeLists.txt`` file and all sources. The ``<bindir>`` and ``<srcdir>``
  17. will not be deleted after this command is run. Specify ``<targetName>`` to
  18. build a specific target instead of the ``all`` or ``ALL_BUILD`` target. See
  19. below for the meaning of other options.
  20. .. versionchanged:: 3.24
  21. CMake variables describing platform settings, and those listed by the
  22. :variable:`CMAKE_TRY_COMPILE_PLATFORM_VARIABLES` variable, are propagated
  23. into the project's build configuration. See policy :policy:`CMP0137`.
  24. Previously this was only done by the
  25. :ref:`source file <Try Compiling Source Files>` signature.
  26. .. _`Try Compiling Source Files`:
  27. Try Compiling Source Files
  28. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  29. .. code-block:: cmake
  30. try_compile(<resultVar> SOURCES <srcfile...>
  31. [CMAKE_FLAGS <flags>...]
  32. [COMPILE_DEFINITIONS <defs>...]
  33. [LINK_OPTIONS <options>...]
  34. [LINK_LIBRARIES <libs>...]
  35. [OUTPUT_VARIABLE <var>]
  36. [COPY_FILE <fileName> [COPY_FILE_ERROR <var>]]
  37. [<LANG>_STANDARD <std>]
  38. [<LANG>_STANDARD_REQUIRED <bool>]
  39. [<LANG>_EXTENSIONS <bool>]
  40. )
  41. .. versionadded:: 3.25
  42. Try building an executable or static library from one or more source files
  43. (which one is determined by the :variable:`CMAKE_TRY_COMPILE_TARGET_TYPE`
  44. variable). The success or failure of the ``try_compile``, i.e. ``TRUE`` or
  45. ``FALSE`` respectively, is returned in ``<resultVar>``.
  46. In this form, one or more source files must be provided. If
  47. :variable:`CMAKE_TRY_COMPILE_TARGET_TYPE` is unset or is set to ``EXECUTABLE``,
  48. the sources must include a definition for ``main`` and CMake will create a
  49. ``CMakeLists.txt`` file to build the source(s) as an executable.
  50. If :variable:`CMAKE_TRY_COMPILE_TARGET_TYPE` is set to ``STATIC_LIBRARY``,
  51. a static library will be built instead and no definition for ``main`` is
  52. required. For an executable, the generated ``CMakeLists.txt`` file would
  53. contain something like the following:
  54. .. code-block:: cmake
  55. add_definitions(<expanded COMPILE_DEFINITIONS from caller>)
  56. include_directories(${INCLUDE_DIRECTORIES})
  57. link_directories(${LINK_DIRECTORIES})
  58. add_executable(cmTryCompileExec <srcfile>...)
  59. target_link_options(cmTryCompileExec PRIVATE <LINK_OPTIONS from caller>)
  60. target_link_libraries(cmTryCompileExec ${LINK_LIBRARIES})
  61. CMake will automatically generate a unique directory for each ``try_compile``
  62. operation in an unspecified location within the project's binary directory.
  63. These directories will be cleaned automatically unless
  64. :option:`--debug-trycompile <cmake --debug-trycompile>` is passed to ``cmake``.
  65. Such directories from previous runs are also unconditionally cleaned at the
  66. beginning of any ``cmake`` execution.
  67. This command also supports an alternate signature
  68. which was present in older versions of CMake:
  69. .. code-block:: cmake
  70. try_compile(<resultVar> <bindir> <srcfile|SOURCES srcfile...>
  71. [CMAKE_FLAGS <flags>...]
  72. [COMPILE_DEFINITIONS <defs>...]
  73. [LINK_OPTIONS <options>...]
  74. [LINK_LIBRARIES <libs>...]
  75. [OUTPUT_VARIABLE <var>]
  76. [COPY_FILE <fileName> [COPY_FILE_ERROR <var>]]
  77. [<LANG>_STANDARD <std>]
  78. [<LANG>_STANDARD_REQUIRED <bool>]
  79. [<LANG>_EXTENSIONS <bool>]
  80. )
  81. In this version, ``try_compile`` will use ``<bindir>/CMakeFiles/CMakeTmp`` for
  82. its operation, and all such files will be cleaned automatically.
  83. For debugging, :option:`--debug-trycompile <cmake --debug-trycompile>` can be
  84. passed to ``cmake`` to avoid this clean. However, multiple sequential
  85. ``try_compile`` operations, if given the same ``<bindir>``, will reuse this
  86. single output directory, such that you can only debug one such ``try_compile``
  87. call at a time. Use of the newer signature is recommended to simplify
  88. debugging of multiple ``try_compile`` operations.
  89. The options are:
  90. ``CMAKE_FLAGS <flags>...``
  91. Specify flags of the form :option:`-DVAR:TYPE=VALUE <cmake -D>` to be passed
  92. to the :manual:`cmake(1)` command-line used to drive the test build.
  93. The above example shows how values for variables
  94. ``INCLUDE_DIRECTORIES``, ``LINK_DIRECTORIES``, and ``LINK_LIBRARIES``
  95. are used.
  96. ``COMPILE_DEFINITIONS <defs>...``
  97. Specify ``-Ddefinition`` arguments to pass to :command:`add_definitions`
  98. in the generated test project.
  99. ``COPY_FILE <fileName>``
  100. Copy the built executable or static library to the given ``<fileName>``.
  101. ``COPY_FILE_ERROR <var>``
  102. Use after ``COPY_FILE`` to capture into variable ``<var>`` any error
  103. message encountered while trying to copy the file.
  104. ``LINK_LIBRARIES <libs>...``
  105. Specify libraries to be linked in the generated project.
  106. The list of libraries may refer to system libraries and to
  107. :ref:`Imported Targets <Imported Targets>` from the calling project.
  108. If this option is specified, any ``-DLINK_LIBRARIES=...`` value
  109. given to the ``CMAKE_FLAGS`` option will be ignored.
  110. ``LINK_OPTIONS <options>...``
  111. .. versionadded:: 3.14
  112. Specify link step options to pass to :command:`target_link_options` or to
  113. set the :prop_tgt:`STATIC_LIBRARY_OPTIONS` target property in the generated
  114. project, depending on the :variable:`CMAKE_TRY_COMPILE_TARGET_TYPE` variable.
  115. ``OUTPUT_VARIABLE <var>``
  116. Store the output from the build process in the given variable.
  117. ``<LANG>_STANDARD <std>``
  118. .. versionadded:: 3.8
  119. Specify the :prop_tgt:`C_STANDARD`, :prop_tgt:`CXX_STANDARD`,
  120. :prop_tgt:`OBJC_STANDARD`, :prop_tgt:`OBJCXX_STANDARD`,
  121. or :prop_tgt:`CUDA_STANDARD` target property of the generated project.
  122. ``<LANG>_STANDARD_REQUIRED <bool>``
  123. .. versionadded:: 3.8
  124. Specify the :prop_tgt:`C_STANDARD_REQUIRED`,
  125. :prop_tgt:`CXX_STANDARD_REQUIRED`, :prop_tgt:`OBJC_STANDARD_REQUIRED`,
  126. :prop_tgt:`OBJCXX_STANDARD_REQUIRED`,or :prop_tgt:`CUDA_STANDARD_REQUIRED`
  127. target property of the generated project.
  128. ``<LANG>_EXTENSIONS <bool>``
  129. .. versionadded:: 3.8
  130. Specify the :prop_tgt:`C_EXTENSIONS`, :prop_tgt:`CXX_EXTENSIONS`,
  131. :prop_tgt:`OBJC_EXTENSIONS`, :prop_tgt:`OBJCXX_EXTENSIONS`,
  132. or :prop_tgt:`CUDA_EXTENSIONS` target property of the generated project.
  133. Other Behavior Settings
  134. ^^^^^^^^^^^^^^^^^^^^^^^
  135. .. versionadded:: 3.4
  136. If set, the following variables are passed in to the generated
  137. try_compile CMakeLists.txt to initialize compile target properties with
  138. default values:
  139. * :variable:`CMAKE_CUDA_RUNTIME_LIBRARY`
  140. * :variable:`CMAKE_ENABLE_EXPORTS`
  141. * :variable:`CMAKE_LINK_SEARCH_START_STATIC`
  142. * :variable:`CMAKE_LINK_SEARCH_END_STATIC`
  143. * :variable:`CMAKE_MSVC_RUNTIME_LIBRARY`
  144. * :variable:`CMAKE_POSITION_INDEPENDENT_CODE`
  145. * :variable:`CMAKE_WATCOM_RUNTIME_LIBRARY`
  146. If :policy:`CMP0056` is set to ``NEW``, then
  147. :variable:`CMAKE_EXE_LINKER_FLAGS` is passed in as well.
  148. .. versionchanged:: 3.14
  149. If :policy:`CMP0083` is set to ``NEW``, then in order to obtain correct
  150. behavior at link time, the ``check_pie_supported()`` command from the
  151. :module:`CheckPIESupported` module must be called before using the
  152. :command:`try_compile` command.
  153. The current settings of :policy:`CMP0065` and :policy:`CMP0083` are propagated
  154. through to the generated test project.
  155. Set the :variable:`CMAKE_TRY_COMPILE_CONFIGURATION` variable to choose
  156. a build configuration.
  157. .. versionadded:: 3.6
  158. Set the :variable:`CMAKE_TRY_COMPILE_TARGET_TYPE` variable to specify
  159. the type of target used for the source file signature.
  160. .. versionadded:: 3.6
  161. Set the :variable:`CMAKE_TRY_COMPILE_PLATFORM_VARIABLES` variable to specify
  162. variables that must be propagated into the test project. This variable is
  163. meant for use only in toolchain files and is only honored by the
  164. ``try_compile()`` command for the source files form, not when given a whole
  165. project.
  166. .. versionchanged:: 3.8
  167. If :policy:`CMP0067` is set to ``NEW``, or any of the ``<LANG>_STANDARD``,
  168. ``<LANG>_STANDARD_REQUIRED``, or ``<LANG>_EXTENSIONS`` options are used,
  169. then the language standard variables are honored:
  170. * :variable:`CMAKE_C_STANDARD`
  171. * :variable:`CMAKE_C_STANDARD_REQUIRED`
  172. * :variable:`CMAKE_C_EXTENSIONS`
  173. * :variable:`CMAKE_CXX_STANDARD`
  174. * :variable:`CMAKE_CXX_STANDARD_REQUIRED`
  175. * :variable:`CMAKE_CXX_EXTENSIONS`
  176. * :variable:`CMAKE_OBJC_STANDARD`
  177. * :variable:`CMAKE_OBJC_STANDARD_REQUIRED`
  178. * :variable:`CMAKE_OBJC_EXTENSIONS`
  179. * :variable:`CMAKE_OBJCXX_STANDARD`
  180. * :variable:`CMAKE_OBJCXX_STANDARD_REQUIRED`
  181. * :variable:`CMAKE_OBJCXX_EXTENSIONS`
  182. * :variable:`CMAKE_CUDA_STANDARD`
  183. * :variable:`CMAKE_CUDA_STANDARD_REQUIRED`
  184. * :variable:`CMAKE_CUDA_EXTENSIONS`
  185. Their values are used to set the corresponding target properties in
  186. the generated project (unless overridden by an explicit option).
  187. .. versionchanged:: 3.14
  188. For the :generator:`Green Hills MULTI` generator the GHS toolset and target
  189. system customization cache variables are also propagated into the test project.
  190. .. versionadded:: 3.24
  191. The :variable:`CMAKE_TRY_COMPILE_NO_PLATFORM_VARIABLES` variable may be
  192. set to disable passing platform variables into the test project.
  193. .. versionadded:: 3.25
  194. If :policy:`CMP0141` is set to ``NEW``, one can use
  195. :variable:`CMAKE_MSVC_DEBUG_INFORMATION_FORMAT` to specify MSVC debug
  196. information format.