try_compile.rst 16 KB

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