try_compile.rst 15 KB

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