Adding Export Configuration.rst 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. :language: cmake
  19. :start-after: # install rules
  20. Now that we have ``MathFunctions`` being exported, we also need to explicitly
  21. install the generated ``MathFunctionsTargets.cmake`` file. This is done by
  22. adding the following to the bottom of the top-level ``CMakeLists.txt``:
  23. .. literalinclude:: Complete/CMakeLists.txt
  24. :caption: CMakeLists.txt
  25. :language: cmake
  26. :start-after: # install the configuration targets
  27. :end-before: include(CMakePackageConfigHelpers)
  28. At this point you should try and run CMake. If everything is setup properly
  29. you will see that CMake will generate an error that looks like:
  30. .. code-block:: console
  31. Target "MathFunctions" INTERFACE_INCLUDE_DIRECTORIES property contains
  32. path:
  33. "/Users/robert/Documents/CMakeClass/Tutorial/Step11/MathFunctions"
  34. which is prefixed in the source directory.
  35. What CMake is trying to say is that during generating the export information
  36. it will export a path that is intrinsically tied to the current machine and
  37. will not be valid on other machines. The solution to this is to update the
  38. ``MathFunctions`` :command:`target_include_directories` to understand that it
  39. needs different ``INTERFACE`` locations when being used from within the build
  40. directory and from an install / package. This means converting the
  41. :command:`target_include_directories` call for ``MathFunctions`` to look like:
  42. .. literalinclude:: Step12/MathFunctions/CMakeLists.txt
  43. :caption: MathFunctions/CMakeLists.txt
  44. :language: cmake
  45. :start-after: # to find MathFunctions.h, while we don't.
  46. :end-before: # should we use our own math functions
  47. Once this has been updated, we can re-run CMake and verify that it doesn't
  48. warn anymore.
  49. At this point, we have CMake properly packaging the target information that is
  50. required but we will still need to generate a ``MathFunctionsConfig.cmake`` so
  51. that the CMake :command:`find_package` command can find our project. So let's go
  52. ahead and add a new file to the top-level of the project called
  53. ``Config.cmake.in`` with the following contents:
  54. .. literalinclude:: Step12/Config.cmake.in
  55. :caption: Config.cmake.in
  56. Then, to properly configure and install that file, add the following to the
  57. bottom of the top-level ``CMakeLists.txt``:
  58. .. literalinclude:: Step12/CMakeLists.txt
  59. :caption: CMakeLists.txt
  60. :language: cmake
  61. :start-after: # install the configuration targets
  62. :end-before: # generate the export
  63. At this point, we have generated a relocatable CMake Configuration for our
  64. project that can be used after the project has been installed or packaged. If
  65. we want our project to also be used from a build directory we only have to add
  66. the following to the bottom of the top level ``CMakeLists.txt``:
  67. .. literalinclude:: Step12/CMakeLists.txt
  68. :caption: CMakeLists.txt
  69. :language: cmake
  70. :start-after: # needs to be after the install(TARGETS ) command
  71. With this export call we now generate a ``Targets.cmake``, allowing the
  72. configured ``MathFunctionsConfig.cmake`` in the build directory to be used by
  73. other projects, without needing it to be installed.