try_compile.rst 10 KB

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