try_compile.rst 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. try_compile
  2. -----------
  3. Try building some code.
  4. ::
  5. try_compile(RESULT_VAR <bindir> <srcdir>
  6. <projectName> [targetName] [CMAKE_FLAGS flags...]
  7. [OUTPUT_VARIABLE <var>])
  8. Try building a project. In this form, srcdir should contain a
  9. complete CMake project with a CMakeLists.txt file and all sources.
  10. The bindir and srcdir will not be deleted after this command is run.
  11. Specify targetName to build a specific target instead of the 'all' or
  12. 'ALL_BUILD' target.
  13. ::
  14. try_compile(RESULT_VAR <bindir> <srcfile|SOURCES srcfile...>
  15. [CMAKE_FLAGS flags...]
  16. [COMPILE_DEFINITIONS flags...]
  17. [LINK_LIBRARIES libs...]
  18. [OUTPUT_VARIABLE <var>]
  19. [COPY_FILE <fileName> [COPY_FILE_ERROR <var>]])
  20. Try building an executable from one or more source files. In this
  21. form the user need only supply one or more source files that include a
  22. definition for 'main'. CMake will create a CMakeLists.txt file to
  23. build the source(s) as an executable. Specify COPY_FILE to get a copy
  24. of the linked executable at the given fileName and optionally
  25. COPY_FILE_ERROR to capture any error.
  26. In this version all files in bindir/CMakeFiles/CMakeTmp will be
  27. cleaned automatically. For debugging, --debug-trycompile can be
  28. passed to cmake to avoid this clean. However, multiple sequential
  29. try_compile operations reuse this single output directory. If you use
  30. --debug-trycompile, you can only debug one try_compile call at a time.
  31. The recommended procedure is to configure with cmake all the way
  32. through once, then delete the cache entry associated with the
  33. try_compile call of interest, and then re-run cmake again with
  34. --debug-trycompile.
  35. Some extra flags that can be included are, INCLUDE_DIRECTORIES,
  36. LINK_DIRECTORIES, and LINK_LIBRARIES. COMPILE_DEFINITIONS are
  37. -Ddefinition that will be passed to the compile line.
  38. The srcfile signature also accepts a LINK_LIBRARIES argument which may
  39. contain a list of libraries or IMPORTED targets which will be linked
  40. to in the generated project. If LINK_LIBRARIES is specified as a
  41. parameter to try_compile, then any LINK_LIBRARIES passed as
  42. CMAKE_FLAGS will be ignored.
  43. try_compile creates a CMakeList.txt file on the fly that looks like
  44. this:
  45. ::
  46. add_definitions( <expanded COMPILE_DEFINITIONS from calling cmake>)
  47. include_directories(${INCLUDE_DIRECTORIES})
  48. link_directories(${LINK_DIRECTORIES})
  49. add_executable(cmTryCompileExec sources)
  50. target_link_libraries(cmTryCompileExec ${LINK_LIBRARIES})
  51. In both versions of the command, if OUTPUT_VARIABLE is specified, then
  52. the output from the build process is stored in the given variable.
  53. The success or failure of the try_compile, i.e. TRUE or FALSE
  54. respectively, is returned in RESULT_VAR. CMAKE_FLAGS can be used to
  55. pass -DVAR:TYPE=VALUE flags to the cmake that is run during the build.
  56. Set variable CMAKE_TRY_COMPILE_CONFIGURATION to choose a build
  57. configuration.