1
0
mirror of synced 2024-11-06 04:58:54 +01:00
modern-cmake/chapters/install/exporting.md

25 lines
1.7 KiB
Markdown
Raw Normal View History

2018-03-31 07:58:58 +02:00
# Exporting
2019-07-19 17:15:11 +02:00
{% hint style='danger' %}
2020-06-09 16:59:07 +02:00
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 later, this will no longer happen unless you set `CMAKE_EXPORT_PACKAGE_REGISTRY` as shown below.
2019-07-19 17:15:11 +02:00
{% endhint %}
2018-03-31 07:58:58 +02:00
2020-06-09 16:59:07 +02:00
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 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.
2019-07-19 17:15:11 +02:00
You should make an export set, probably near the end of your main `CMakeLists.txt`:
2018-03-31 07:58:58 +02:00
```cmake
export(TARGETS MyLib1 MyLib2 NAMESPACE MyLib:: FILE MyLibTargets.cmake)
```
2019-07-19 17:15:11 +02:00
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:
2018-03-31 07:58:58 +02:00
```cmake
2019-07-19 17:15:11 +02:00
set(CMAKE_EXPORT_PACKAGE_REGISTRY ON)
2018-03-31 07:58:58 +02:00
export(PACKAGE MyLib)
```
2019-07-19 17:15:11 +02:00
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.
2018-03-31 07:58:58 +02:00
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.