try_compile.rst 13 KB

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