3

I am creating a header only library (Library A) that includes another library (Library B) using its FindB.cmake file.

I include the FindB.cmake file by having it in a cmake directory and doing:

set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})

find_package(B REQUIRED)

target_link_libraries(A INTERFACE B::B)

The tests on Library A all work fine so B is included successfully in A. To make B available to projects that want to include A, I have in my AConfig.cmake.in :

include(CMakeFindDependencyMacro)
find_dependency(B REQUIRED)

But when I try to make an application (App C) that uses Library A (with find_package(A REQUIRED)), I get an error that cmake cannot find the FindB.cmake file. The error goes away if I put the FindB.cmake file in the App C project and include it like I did with Library A, but I don't want people using Library A to need the FindB.cmake file.

Is there a way to make all of the location information from FindB.cmake carry over from Library A to App C without needing to include the actual FindB.cmake file?

1
  • 3
    Just add script FindB.cmake into the installation of the library A (e.g. via install(FILES)). Note, that results of that script are specific for the machine where it is run. On that machine the script is known to be worked, so you could run the script again, as a part of AConfig.cmake. It is true that on other machine the script could be absent. But that other machine could have Library B installed under other location, so result of that script on initial machine cannot be used. Commented Nov 3, 2021 at 9:10

1 Answer 1

1

@Tsyvarev had the answer.

I added this to install the FindB.cmake:

install(FILES "${PROJECT_SOURCE_DIR}/cmake/FindB.cmake"
        DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/A/cmake
)

Then in my AConfig.cmake.in, I edited it with the following:

set(CMAKE_MODULE_PATH @CMAKE_INSTALL_DATAROOTDIR@/A/cmake @CMAKE_MODULE_PATH@)

find_dependency(B REQUIRED)

I can now add Library A to App C with find_package(A REQUIRED) and everything transfers over.

Thanks again @Tsyvarev

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.