find_package NAMES <PackageName>_FOUND

I am asking to help me for some find_package behavior for two packages:

  • PackageName1: a system supplied package
  • PackageName2: self build newer version of PackageName1

If I apply the find_package command with:
find_package(PackageName1 CONFIG)
I get PackageName1_FOUND with value TRUE as expected.

If I do a similar find to prefer the self compiled package using:
find_package(PackageName1 NAMES PackageName2 CONFIG)
I get:
PackageName1_FOUND = FALSE
PackageName2_FOUND =

The documentation states … If the NAMES option is given, the names following it are used instead of PackageName. … . Therefore I expected to get at least:
PackageName2_FOUND = TRUE
PackageName1_FOUND = TRUE
PackageName2_FOUND = TRUE

Am I doing something wrong or interpret the subject false?

I appreciate any help to clarify the subject. Thanks in advance.

1 Like

The found package might not use CMAKE_FIND_PACKAGE_NAME when setting variables and instead only sets PackageName1_FOUND statically.

The XXX_FOUND variable should always use the first argument to find_package() as the XXX prefix. The NAMES are irrelevant as far as the output variable is concerned, those names are only to guide the search. For your specific example, you are only calling find_package() with PackageName1 as the first argument, so you should only be looking at PackageName1_FOUND. You should not take any notice of whether PackageName2_FOUND is set or not. If it is, that’s an implementation detail that you shouldn’t be relying on (in so far as you’ve asked to get the result reported back through PackageName1_FOUND since that’s your first argument to find_package()).

Wasn’t aware of CMAKE_FIND_PACKAGE_NAME at all and will use that in future pkg config files. Thanks!

Having your answer in mind the behavior is clear for me now! Thanks!