Thanks to @Tsyvarev's comments I found the problem and got the solution I share here in case someone finds a similar error:
Problems:
Foo_LIBRARY
var was empty so the target library was not created correctlyGLOBAL
so the scope of the target library was only the current fileFinally 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.