NauticalMile
NauticalMile

Reputation: 1805

In CMake, how can I find the imported target after calling find_package?

CMake find_package documentation says:

When the package is found package-specific information is provided through variables and Imported Targets documented by the package itself.

If I have a library, ABCD which I can get using find_package, provided CMake can locate a suitable ABCDConfig.cmake file. It then creates the Imported Target(s) (e.g. abcd10 abcdExtra) as mentioned above.

Then I can compile and link against these libraries like so:

target_include_directories(mytarget abcd10 abcdExtra)
target_link_libraries(mytarget abcd10 abcdExtra)

The problem is, depending on how the contents of that ABCDConfig.cmake file, which somebody else wrote, the names of the targets could be almost anything (e.g. abcdlib,abcdX64,Foobar,...), even though it might be the exact same library ABCD!

I've Looked through much of the documentation, but I can't see any methods to get the Imported Targets, so it looks like I have to know what all the common imported target names are, and check for them individually. This seems a little bit unreasonable to me. Am I missing something here? How can I load and link against libraries generated by different package managers (or even someone who wrote their own ABCDConfig.cmake file) without knowing these specifics?

Upvotes: 6

Views: 1626

Answers (1)

Tsyvarev
Tsyvarev

Reputation: 66061

How can I load and link against libraries generated by different package managers (or even someone who wrote their own ABCDConfig.cmake file) without knowing these specifics?

You cannot.

The only way is to read documentation about specific ABCDConfig.cmake file. Such documentation is usually written at the beginning of the file (as comments).

CMake doesn't enforce anything about ABCDConfig.cmake files, so only the author of the file knows how to use it.

While CMake provides some support for generating config files (see install(EXPORTS) command), only the author of the project knows meaning of the imported targets.

Upvotes: 6

Related Questions