From 70aba15474de40c93a0889b479b024fbdd915210 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Fri, 19 Jul 2019 11:15:11 -0400 Subject: [PATCH] Adding note on exporting --- chapters/install/exporting.md | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/chapters/install/exporting.md b/chapters/install/exporting.md index ba4d8df..500f20d 100644 --- a/chapters/install/exporting.md +++ b/chapters/install/exporting.md @@ -1,20 +1,25 @@ # Exporting -There are two ways to access a project from another project. First is exporting targets and using them in the build directory. It usually is simply a by-product of setting up the third method (installing), but is useful for development and as a way to prepare to discuss the installation procedure. +{% hint style='danger' %} +The default behavior for exporting changed in CMake 3.15. Since changing files in a user's home directory is considered "surprising" (and it is, which is why this chapter exists), it is no longer the default behavior. If you set a minimum or maximum CMake version of 3.15 or better, this will no longer happen unless you set `CMAKE_EXPORT_PACKAGE_REGISTRY`, as mentioned below. +{% endhint %} -First, you should make an export set, probably near the end of your main `CMakeLists.txt`: +There are three ways to access a project from another project: subdirectory, exported build directories, and installing. To use the build directory of one project in another project, you will need to export targets. Exporting targets is needed for a proper install, allowing the build directory to be used as well is just two added lines. It is not generally a way to work that I would recommend, but can be useful for development and as way to prepare the installation procedure discussed later. + +You should make an export set, probably near the end of your main `CMakeLists.txt`: ```cmake export(TARGETS MyLib1 MyLib2 NAMESPACE MyLib:: FILE MyLibTargets.cmake) ``` -This puts the targets you've listed into a file in the build directory, and optionally prefixes them with a namespace. Now, to allow CMake to find this package, export the package into the `$HOME/.cmake/packages` folder: +This puts the targets you have listed into a file in the build directory, and optionally prefixes them with a namespace. Now, to allow CMake to find this package, export the package into the `$HOME/.cmake/packages` folder: ```cmake +set(CMAKE_EXPORT_PACKAGE_REGISTRY ON) export(PACKAGE MyLib) ``` -Now, if you `find_package(MyLib)`, CMake can find the build folder. Look at the generated MyLibTargets.cmake file to help you understand exactly what is created; it's just a normal CMake file, with the exported targets. +Now, if you `find_package(MyLib)`, CMake can find the build folder. Look at the generated `MyLibTargets.cmake` file to help you understand exactly what is created; it's just a normal CMake file, with the exported targets. Note that there's a downside: if you have imported dependencies, they will need to be imported before you `find_package`. That will be fixed in the next method.