Use exported targets and find_package on build path


I’m trying to understand the whole packaging process. For this, I created a project with the following folder structure

main folder
– lib
– – lib_1
– – – CMakeLists.txt
– – – …
– – lib_2
– – – CMakeLists.txt
– – – …

I managed to set everything up, so that after installing, I can load the libraries using
find_package(MainProjectName REQUIRED COMPONENTS lib_1 lib_2)

Now, I read that it is useful (and I see the advantage) to provide the user with the possibility to import the targets from the build-path.

I don’t get this to work - my problem is the following: if I use find_package on my build-folder it finds the lib_x-config.cmake files, but not the automatically exported lib_x-targets.cmake files. In the installation folder the target files are in the same folder as the config files. In the build folder, however, they are in the subfolder “CMakeFiles/Export/lib/lib_x”.
Can I somehow specify where the target-files are stored?

In my config-files I use
to load the targets - is it possible to specify different paths depending on whether the build or install folder is used (maybe with one of these commands in angle brackets).

Thanks and best regards

The export() command exports targets for the build tree. You can tell it to put the file wherever you need.