Adding Export Configuration.rst 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. Step 11: Adding Export Configuration
  2. ====================================
  3. During :guide:`tutorial/Installing and Testing` of the tutorial we added the
  4. ability for CMake to install the library and headers of the project. During
  5. :guide:`tutorial/Packaging an Installer` we added the ability to package up
  6. this information so it could be distributed to other people.
  7. The next step is to add the necessary information so that other CMake projects
  8. can use our project, be it from a build directory, a local install or when
  9. packaged.
  10. The first step is to update our :command:`install(TARGETS)` commands to not
  11. only specify a ``DESTINATION`` but also an ``EXPORT``. The ``EXPORT`` keyword
  12. generates and installs a CMake file containing code to import all targets
  13. listed in the install command from the installation tree. So let's go ahead and
  14. explicitly ``EXPORT`` the ``MathFunctions`` library by updating the ``install``
  15. command in ``MathFunctions/CMakeLists.txt`` to look like:
  16. .. literalinclude:: Complete/MathFunctions/CMakeLists.txt
  17. :caption: MathFunctions/CMakeLists.txt
  18. :name: MathFunctions/CMakeLists.txt-install-TARGETS-EXPORT
  19. :language: cmake
  20. :start-after: # install rules
  21. Now that we have ``MathFunctions`` being exported, we also need to explicitly
  22. install the generated ``MathFunctionsTargets.cmake`` file. This is done by
  23. adding the following to the bottom of the top-level ``CMakeLists.txt``:
  24. .. literalinclude:: Complete/CMakeLists.txt
  25. :caption: CMakeLists.txt
  26. :name: CMakeLists.txt-install-EXPORT
  27. :language: cmake
  28. :start-after: # install the configuration targets
  29. :end-before: include(CMakePackageConfigHelpers)
  30. At this point you should try and run CMake. If everything is setup properly
  31. you will see that CMake will generate an error that looks like:
  32. .. code-block:: console
  33. Target "MathFunctions" INTERFACE_INCLUDE_DIRECTORIES property contains
  34. path:
  35. "/Users/robert/Documents/CMakeClass/Tutorial/Step11/MathFunctions"
  36. which is prefixed in the source directory.
  37. What CMake is trying to say is that during generating the export information
  38. it will export a path that is intrinsically tied to the current machine and
  39. will not be valid on other machines. The solution to this is to update the
  40. ``MathFunctions`` :command:`target_include_directories` to understand that it
  41. needs different ``INTERFACE`` locations when being used from within the build
  42. directory and from an install / package. This means converting the
  43. :command:`target_include_directories` call for ``MathFunctions`` to look like:
  44. .. literalinclude:: Step12/MathFunctions/CMakeLists.txt
  45. :caption: MathFunctions/CMakeLists.txt
  46. :name: MathFunctions/CMakeLists.txt-target_include_directories
  47. :language: cmake
  48. :start-after: # to find MathFunctions.h, while we don't.
  49. :end-before: # should we use our own math functions
  50. Once this has been updated, we can re-run CMake and verify that it doesn't
  51. warn anymore.
  52. At this point, we have CMake properly packaging the target information that is
  53. required but we will still need to generate a ``MathFunctionsConfig.cmake`` so
  54. that the CMake :command:`find_package` command can find our project. So let's go
  55. ahead and add a new file to the top-level of the project called
  56. ``Config.cmake.in`` with the following contents:
  57. .. literalinclude:: Step12/Config.cmake.in
  58. :caption: Config.cmake.in
  59. :name: Config.cmake.in
  60. Then, to properly configure and install that file, add the following to the
  61. bottom of the top-level ``CMakeLists.txt``:
  62. .. literalinclude:: Step12/CMakeLists.txt
  63. :caption: CMakeLists.txt
  64. :name: CMakeLists.txt-install-Config.cmake
  65. :language: cmake
  66. :start-after: # install the configuration targets
  67. :end-before: # generate the export
  68. At this point, we have generated a relocatable CMake Configuration for our
  69. project that can be used after the project has been installed or packaged. If
  70. we want our project to also be used from a build directory we only have to add
  71. the following to the bottom of the top level ``CMakeLists.txt``:
  72. .. literalinclude:: Step12/CMakeLists.txt
  73. :caption: CMakeLists.txt
  74. :name: CMakeLists.txt-export
  75. :language: cmake
  76. :start-after: # needs to be after the install(TARGETS ) command
  77. With this export call we now generate a ``Targets.cmake``, allowing the
  78. configured ``MathFunctionsConfig.cmake`` in the build directory to be used by
  79. other projects, without needing it to be installed.