pkg_check_modules() imported target does not propagate

Greetings,

I have an issue with pkg_check_modules() (cmake version 3.22.3) and I seem not to be able to solve it (been trying for more than a week).

I have two CMake projects, lets call them A and B.

Project A

add_library(targetA SHARED ...)
pkg_check_modules(GST REQUIRED IMPORTED_TARGET GLOBAL gstreamer-1.0)
target_link_libraries(targetA PUBLIC PkgConfig::GST)

# here comes installing the ATargets.cmake and AConfig.cmake

Project B

Project B depends on Project A by using the targetA. Basically, it does the following:

find_package(A)

Issue

The issue I am having is that the generated ATargets.cmake file contains the following line:

set_target_properties(A::targetA PROPERTIES
  INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include"
  INTERFACE_LINK_LIBRARIES "PkgConfig::GST"
)

…so when building project B, the find_package(A) call fails with the error message:

The link interface of target "A::targetA" contains:
|
|     PkgConfig::GST
|
|   but the target was not found.  Possible reasons include:
|
|     * There is a typo in the target name.
|     * A find_package call is missing for an IMPORTED target.
|     * An ALIAS target is missing.

According to the pkg_check_modules documentation, the call creates the PkgConfig::GST imported target, but I don’t quite understand what this means. Where is the target, I can’t seem to find any files being generated. I was trying to install this created PkgConfig::GST target, but with no luck.

Thanks for any support on this.

I’ve never used pkg_check_modules(), but I would assume the logic is the same, and so in your Project B before you do find_package(A) there should be that same line with finding gstreamer-1.0 using pkg_check_modules(), or alternatively your AConfig.cmake should contain something of the sort (with CMake packages it would be find_dependency()).

Thanks for the reply, I went into this direction and believe this is a right solution. I’ll describe it below, maybe someone wants to chime in or will find it useful.

The issue I had (PkgConfig::GST target is missing) was because when Project A is imported by another project, this target does not exist anymore and must be re-generated. That’s why I added the below line to the AConfig.cmake file (during the build):

find_package(PkgConfig)
pkg_check_modules(GST REQUIRED IMPORTED_TARGET GLOBAL gstreamer-1.0)

This means that every project which calls find_package(A) indirectly creates the PkgConfig::GST target, so using this target through ATargets.cmake won’t fail.

PS: If you have mulitple pkg_check_modules calls, don’t forget that the second call will invalidate the PkgConfig::GST target created by the first call.

Aleksandar