try_compile.rst 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. try_compile
  2. -----------
  3. .. only:: html
  4. .. contents::
  5. Try building some code.
  6. Try Compiling Whole Projects
  7. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  8. ::
  9. try_compile(RESULT_VAR <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 ``RESULT_VAR``.
  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. ::
  22. try_compile(RESULT_VAR <bindir> <srcfile|SOURCES srcfile...>
  23. [CMAKE_FLAGS <flags>...]
  24. [COMPILE_DEFINITIONS <defs>...]
  25. [LINK_LIBRARIES <libs>...]
  26. [OUTPUT_VARIABLE <var>]
  27. [COPY_FILE <fileName> [COPY_FILE_ERROR <var>]])
  28. Try building an executable from one or more source files. The success or
  29. failure of the ``try_compile``, i.e. ``TRUE`` or ``FALSE`` respectively, is
  30. returned in ``RESULT_VAR``.
  31. In this form the user need only supply one or more source files that include a
  32. definition for ``main``. CMake will create a ``CMakeLists.txt`` file to build
  33. the source(s) as an executable that looks something like this::
  34. add_definitions(<expanded COMPILE_DEFINITIONS from caller>)
  35. include_directories(${INCLUDE_DIRECTORIES})
  36. link_directories(${LINK_DIRECTORIES})
  37. add_executable(cmTryCompileExec <srcfile>...)
  38. target_link_libraries(cmTryCompileExec ${LINK_LIBRARIES})
  39. The options are:
  40. ``CMAKE_FLAGS <flags>...``
  41. Specify flags of the form ``-DVAR:TYPE=VALUE`` to be passed to
  42. the ``cmake`` command-line used to drive the test build.
  43. The above example shows how values for variables
  44. ``INCLUDE_DIRECTORIES``, ``LINK_DIRECTORIES``, and ``LINK_LIBRARIES``
  45. are used.
  46. ``COMPILE_DEFINITIONS <defs>...``
  47. Specify ``-Ddefinition`` arguments to pass to ``add_definitions``
  48. in the generated test project.
  49. ``COPY_FILE <fileName>``
  50. Copy the linked executable to the given ``<fileName>``.
  51. ``COPY_FILE_ERROR <var>``
  52. Use after ``COPY_FILE`` to capture into variable ``<var>`` any error
  53. message encountered while trying to copy the file.
  54. ``LINK_LIBRARIES <libs>...``
  55. Specify libraries to be linked in the generated project.
  56. The list of libraries may refer to system libraries and to
  57. :ref:`Imported Targets <Imported Targets>` from the calling project.
  58. If this option is specified, any ``-DLINK_LIBRARIES=...`` value
  59. given to the ``CMAKE_FLAGS`` option will be ignored.
  60. ``OUTPUT_VARIABLE <var>``
  61. Store the output from the build process the given variable.
  62. In this version all files in ``<bindir>/CMakeFiles/CMakeTmp`` will be
  63. cleaned automatically. For debugging, ``--debug-trycompile`` can be
  64. passed to ``cmake`` to avoid this clean. However, multiple sequential
  65. ``try_compile`` operations reuse this single output directory. If you use
  66. ``--debug-trycompile``, you can only debug one ``try_compile`` call at a time.
  67. The recommended procedure is to protect all ``try_compile`` calls in your
  68. project by ``if(NOT DEFINED RESULT_VAR)`` logic, configure with cmake
  69. all the way through once, then delete the cache entry associated with
  70. the try_compile call of interest, and then re-run cmake again with
  71. ``--debug-trycompile``.
  72. Other Behavior Settings
  73. ^^^^^^^^^^^^^^^^^^^^^^^
  74. If set, the following variables are passed in to the generated
  75. try_compile CMakeLists.txt to initialize compile target properties with
  76. default values:
  77. * :variable:`CMAKE_ENABLE_EXPORTS`
  78. * :variable:`CMAKE_LINK_SEARCH_START_STATIC`
  79. * :variable:`CMAKE_LINK_SEARCH_END_STATIC`
  80. * :variable:`CMAKE_POSITION_INDEPENDENT_CODE`
  81. If :policy:`CMP0056` is set to ``NEW``, then
  82. :variable:`CMAKE_EXE_LINKER_FLAGS` is passed in as well.
  83. The current setting of :policy:`CMP0065` is set in the generated project.
  84. Set the :variable:`CMAKE_TRY_COMPILE_CONFIGURATION` variable to choose
  85. a build configuration.