try_compile.rst 8.0 KB

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