How do I export a target, then use it in another project via ExternalProject?

584 Views Asked by At

I have a CMake project named proj1, which I want to use as an external project in another project, proj2. Now, the (relevant) command in proj1's CMakeLists.txt is:

install(
    TARGETS proj1
    ARCHIVE
    DESTINATION lib
    EXPORT proj1_library
    INCLUDES DESTINATION include
    CONFIGURATIONS Release RelWithDebugInfo
)

and I want to use this static library in proj2, without explicitly "guessing" where it's installed to be proj1. I want to be able to get obtain this target from proj1 (which I obtain using ExternalProject), then use it - directly or indirectly - in add_target_libraries() commands.

How should I do that? And - do I only need to make changes to proj2 or also to proj1's CMakeLists.txt?

1

There are 1 best solutions below

1
On

Exporting the targets is the right approach.

To support this, proj1 would have to generate a proj1Config.cmake in both its build tree and also in the install tree (so that a development package for proj1 can be used as a SDK [1])

I suggest you read the following section of the CMake documentation, it covers the different concepts and provides an example. See https://cmake.org/cmake/help/latest/manual/cmake-packages.7.html#creating-packages

[1] SDK: https://en.wikipedia.org/wiki/Software_development_kit