try_compile.rst 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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> <bindir> <srcfile|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. Try building an executable or static library from one or more source files
  42. (which one is determined by the :variable:`CMAKE_TRY_COMPILE_TARGET_TYPE`
  43. variable). The success or failure of the ``try_compile``, i.e. ``TRUE`` or
  44. ``FALSE`` respectively, is returned in ``<resultVar>``.
  45. In this form, one or more source files must be provided. If
  46. :variable:`CMAKE_TRY_COMPILE_TARGET_TYPE` is unset or is set to ``EXECUTABLE``,
  47. the sources must include a definition for ``main`` and CMake will create a
  48. ``CMakeLists.txt`` file to build the source(s) as an executable.
  49. If :variable:`CMAKE_TRY_COMPILE_TARGET_TYPE` is set to ``STATIC_LIBRARY``,
  50. a static library will be built instead and no definition for ``main`` is
  51. required. For an executable, the generated ``CMakeLists.txt`` file would
  52. contain something like the following:
  53. .. code-block:: cmake
  54. add_definitions(<expanded COMPILE_DEFINITIONS from caller>)
  55. include_directories(${INCLUDE_DIRECTORIES})
  56. link_directories(${LINK_DIRECTORIES})
  57. add_executable(cmTryCompileExec <srcfile>...)
  58. target_link_options(cmTryCompileExec PRIVATE <LINK_OPTIONS from caller>)
  59. target_link_libraries(cmTryCompileExec ${LINK_LIBRARIES})
  60. The options are:
  61. ``CMAKE_FLAGS <flags>...``
  62. Specify flags of the form ``-DVAR:TYPE=VALUE`` to be passed to
  63. the ``cmake`` command-line used to drive the test build.
  64. The above example shows how values for variables
  65. ``INCLUDE_DIRECTORIES``, ``LINK_DIRECTORIES``, and ``LINK_LIBRARIES``
  66. are used.
  67. ``COMPILE_DEFINITIONS <defs>...``
  68. Specify ``-Ddefinition`` arguments to pass to :command:`add_definitions`
  69. in the generated test project.
  70. ``COPY_FILE <fileName>``
  71. Copy the built executable or static library to the given ``<fileName>``.
  72. ``COPY_FILE_ERROR <var>``
  73. Use after ``COPY_FILE`` to capture into variable ``<var>`` any error
  74. message encountered while trying to copy the file.
  75. ``LINK_LIBRARIES <libs>...``
  76. Specify libraries to be linked in the generated project.
  77. The list of libraries may refer to system libraries and to
  78. :ref:`Imported Targets <Imported Targets>` from the calling project.
  79. If this option is specified, any ``-DLINK_LIBRARIES=...`` value
  80. given to the ``CMAKE_FLAGS`` option will be ignored.
  81. ``LINK_OPTIONS <options>...``
  82. .. versionadded:: 3.14
  83. Specify link step options to pass to :command:`target_link_options` or to
  84. set the :prop_tgt:`STATIC_LIBRARY_OPTIONS` target property in the generated
  85. project, depending on the :variable:`CMAKE_TRY_COMPILE_TARGET_TYPE` variable.
  86. ``OUTPUT_VARIABLE <var>``
  87. Store the output from the build process in the given variable.
  88. ``<LANG>_STANDARD <std>``
  89. .. versionadded:: 3.8
  90. Specify the :prop_tgt:`C_STANDARD`, :prop_tgt:`CXX_STANDARD`,
  91. :prop_tgt:`OBJC_STANDARD`, :prop_tgt:`OBJCXX_STANDARD`,
  92. or :prop_tgt:`CUDA_STANDARD` target property of the generated project.
  93. ``<LANG>_STANDARD_REQUIRED <bool>``
  94. .. versionadded:: 3.8
  95. Specify the :prop_tgt:`C_STANDARD_REQUIRED`,
  96. :prop_tgt:`CXX_STANDARD_REQUIRED`, :prop_tgt:`OBJC_STANDARD_REQUIRED`,
  97. :prop_tgt:`OBJCXX_STANDARD_REQUIRED`,or :prop_tgt:`CUDA_STANDARD_REQUIRED`
  98. target property of the generated project.
  99. ``<LANG>_EXTENSIONS <bool>``
  100. .. versionadded:: 3.8
  101. Specify the :prop_tgt:`C_EXTENSIONS`, :prop_tgt:`CXX_EXTENSIONS`,
  102. :prop_tgt:`OBJC_EXTENSIONS`, :prop_tgt:`OBJCXX_EXTENSIONS`,
  103. or :prop_tgt:`CUDA_EXTENSIONS` target property of the generated project.
  104. In this version all files in ``<bindir>/CMakeFiles/CMakeTmp`` will be
  105. cleaned automatically. For debugging, ``--debug-trycompile`` can be
  106. passed to ``cmake`` to avoid this clean. However, multiple sequential
  107. ``try_compile`` operations reuse this single output directory. If you use
  108. ``--debug-trycompile``, you can only debug one ``try_compile`` call at a time.
  109. The recommended procedure is to protect all ``try_compile`` calls in your
  110. project by ``if(NOT DEFINED <resultVar>)`` logic, configure with cmake
  111. all the way through once, then delete the cache entry associated with
  112. the try_compile call of interest, and then re-run cmake again with
  113. ``--debug-trycompile``.
  114. Other Behavior Settings
  115. ^^^^^^^^^^^^^^^^^^^^^^^
  116. .. versionadded:: 3.4
  117. If set, the following variables are passed in to the generated
  118. try_compile CMakeLists.txt to initialize compile target properties with
  119. default values:
  120. * :variable:`CMAKE_CUDA_RUNTIME_LIBRARY`
  121. * :variable:`CMAKE_ENABLE_EXPORTS`
  122. * :variable:`CMAKE_LINK_SEARCH_START_STATIC`
  123. * :variable:`CMAKE_LINK_SEARCH_END_STATIC`
  124. * :variable:`CMAKE_MSVC_RUNTIME_LIBRARY`
  125. * :variable:`CMAKE_POSITION_INDEPENDENT_CODE`
  126. * :variable:`CMAKE_WATCOM_RUNTIME_LIBRARY`
  127. If :policy:`CMP0056` is set to ``NEW``, then
  128. :variable:`CMAKE_EXE_LINKER_FLAGS` is passed in as well.
  129. .. versionchanged:: 3.14
  130. If :policy:`CMP0083` is set to ``NEW``, then in order to obtain correct
  131. behavior at link time, the ``check_pie_supported()`` command from the
  132. :module:`CheckPIESupported` module must be called before using the
  133. :command:`try_compile` command.
  134. The current settings of :policy:`CMP0065` and :policy:`CMP0083` are propagated
  135. through to the generated test project.
  136. Set the :variable:`CMAKE_TRY_COMPILE_CONFIGURATION` variable to choose
  137. a build configuration.
  138. .. versionadded:: 3.6
  139. Set the :variable:`CMAKE_TRY_COMPILE_TARGET_TYPE` variable to specify
  140. the type of target used for the source file signature.
  141. .. versionadded:: 3.6
  142. Set the :variable:`CMAKE_TRY_COMPILE_PLATFORM_VARIABLES` variable to specify
  143. variables that must be propagated into the test project. This variable is
  144. meant for use only in toolchain files and is only honored by the
  145. ``try_compile()`` command for the source files form, not when given a whole
  146. project.
  147. .. versionchanged:: 3.8
  148. If :policy:`CMP0067` is set to ``NEW``, or any of the ``<LANG>_STANDARD``,
  149. ``<LANG>_STANDARD_REQUIRED``, or ``<LANG>_EXTENSIONS`` options are used,
  150. then the language standard variables are honored:
  151. * :variable:`CMAKE_C_STANDARD`
  152. * :variable:`CMAKE_C_STANDARD_REQUIRED`
  153. * :variable:`CMAKE_C_EXTENSIONS`
  154. * :variable:`CMAKE_CXX_STANDARD`
  155. * :variable:`CMAKE_CXX_STANDARD_REQUIRED`
  156. * :variable:`CMAKE_CXX_EXTENSIONS`
  157. * :variable:`CMAKE_OBJC_STANDARD`
  158. * :variable:`CMAKE_OBJC_STANDARD_REQUIRED`
  159. * :variable:`CMAKE_OBJC_EXTENSIONS`
  160. * :variable:`CMAKE_OBJCXX_STANDARD`
  161. * :variable:`CMAKE_OBJCXX_STANDARD_REQUIRED`
  162. * :variable:`CMAKE_OBJCXX_EXTENSIONS`
  163. * :variable:`CMAKE_CUDA_STANDARD`
  164. * :variable:`CMAKE_CUDA_STANDARD_REQUIRED`
  165. * :variable:`CMAKE_CUDA_EXTENSIONS`
  166. Their values are used to set the corresponding target properties in
  167. the generated project (unless overridden by an explicit option).
  168. .. versionchanged:: 3.14
  169. For the :generator:`Green Hills MULTI` generator the GHS toolset and target
  170. system customization cache variables are also propagated into the test project.
  171. .. versionadded:: 3.24
  172. The :variable:`CMAKE_TRY_COMPILE_NO_PLATFORM_VARIABLES` variable may be
  173. set to disable passing platform variables into the test project.