79151923

Date: 2024-11-03 02:12:07
Score: 0.5
Natty:
Report link

Thanks to @Tsyvarev's comments I found the problem and got the solution I share here in case someone finds a similar error:

Problems:

  1. Foo_LIBRARY var was empty so the target library was not created correctly
  2. The imported target was not GLOBAL so the scope of the target library was only the current file
  3. It was linking to Foo (and not the other dependencies) just by chance because libFoo.so exists in the system, this is why I thought it was ignoring the properties but what was happening is that the whole target was not existant, treating Foo like a plain library and not a target.

Finally the solution that works to import a library and set its dependencies is:

FindFoo.cmake

# Look for the necessary header
find_path(Foo_INCLUDE_DIR NAMES foo.h)
mark_as_advanced(Foo_INCLUDE_DIR)

# Look for the necessary library
find_library(Foo_LIBRARY NAMES foo)
mark_as_advanced(Foo_LIBRARY)

# Extract version information from the header file
if(Foo_INCLUDE_DIR AND Foo_LIBRARY)
    set(Foo_FOUND TRUE)
    set(Foo_LIBRARIES bar baz)
endif()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Foo
    REQUIRED_VARS Foo_INCLUDE_DIR Foo_LIBRARY Foo_LIBRARIES
)

# Create the imported target
if(Foo_FOUND)
    set(Foo_INCLUDE_DIRS ${StormByte_INCLUDE_DIR})
    if(NOT TARGET Foo)
        add_library(Foo UNKNOWN IMPORTED GLOBAL)
        set_target_properties(Foo PROPERTIES
            IMPORTED_LOCATION                   "${Foo_LIBRARY}"
            INTERFACE_INCLUDE_DIRECTORIES       "${Foo_INCLUDE_DIRS}"
            INTERFACE_LINK_LIBRARIES            "${Foo_LIBRARIES}"
        )
    endif()
endif()

So with this configuration since now the target is GLOBAL every other CMakeFiles.txt can target Foo library with its dependencies with just: target_link_libraries(TARGET Foo) if find_package(Foo) has been executed somewhere.

Note: There is no need to execute find_package(Foo) before or after final target, it will work anyway.

Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @Tsyvarev's
  • Self-answer (0.5):
Posted by: StormByte