Adding Export Configuration.rst 3.8 KB

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