Best way to find_package for subproject?

Project Hierarchy

  • Main CMakeLists.txt
    • App CMakeLists.txt
    • LibA CMakeLists.txt

Details

Main CMakeLists.txt

...
add_subdirectory(LibA)
add_subdirectory(App)
...

App CMakeLists.txt

...
find_package(LibA REQUIRED)
...

Problem

find_package(LibA REQUIRED) in App CMakeLists.txt doesn’t always succeed, cases:

  • As a Visual Studio 2019 cmake proejct it works
  • As a Qt Creator project it fails
  • When configure and generate with cmake-gui it fails

Question

I’m using Qt Creator now, what’s the best way for find_package to work?

Here is a workaround but not good enough: CMake find_package dependency on subproject - Stack Overflow

I think there is no universal way:

  • For a lib with add_subdirectory, target_link is enough.
  • For an external lib, an additional find_package is needed.

Right?

I think you want FetchContent. @craig.scott has resources on its usage in practice (that I should really note down so I don’t keep pinging him for these).

1 Like

The FetchContent module documentation is also where I direct people to for such queries.

2 Likes