0

In my CMakeLists.txt file I’m trying to build “Bob”, which is a library that gets linked to later. The find_package request is successful, but the "if(NOT TARGET..." check fails. I figured if Bob was found then Bob::Bob would also be found. I could obviously be wrong.

What is “if(NOT TARGET Bob::Bob)” using to determine true or false? How do I set it up correctly?

CMakeLists.txt:

find_package(Bob ${BOB_VERSION} EXACT REQUIRED ${CMAKE_PREFIX_PATH})


if (${Bob_FOUND})
    message(STATUS "Found Bob")
else (${Bob_FOUND})
    message(FATAL_ERROR "Could not locate Bob! Abort!")
endif (${Bob_FOUND})


if(NOT TARGET Bob::Bob)
    message(WARNING "Cannot build without Bob::Bob!”)
...

Output:

#13 3.210 Finding Bob...
#13 3.210
#13 3.210 Entered BobConfigVersion.cmake
#13 3.210 Value of BOB_VERSION is: 2022.12.01
#13 3.210    PACKAGE_VERSION           : 2022.12.01
#13 3.210    Bob_VERSION          : 2022.12.01
#13 3.210    PACKAGE_FIND_VERSION      : 2022.12.01
#13 3.210    PACKAGE_VERSION_COMPATIBLE: TRUE
#13 3.210    PACKAGE_VERSION_EXACT     : TRUE
#13 3.210 Exiting BobConfigVersion.cmake

#13 3.213 -- Found Bob
#13 3.320   Cannot build without Bob::Bob!
Kenny Cason
  • 497
  • 5
  • 17
  • Actually, `find_package(Bob)` just executes corresponding `BobConfig.cmake` script. It is up to that script to create a target, to define variables or to do something else. If you wonder how to write such script, then see [CMake documentation](https://cmake.org/cmake/help/latest/manual/cmake-packages.7.html#creating-packages). – Tsyvarev Jan 26 '23 at 23:12
  • It would be better if you'd tell us the exact library name instead of using `Bob`. – Milan Š. Jan 27 '23 at 07:01
  • I've looked at the CMake documentation, but nothing I've found answers the question: What is “if(NOT TARGET Bob::Bob)” using to determine true or false? – Kenny Cason Jan 27 '23 at 16:38

0 Answers0