[CMake] Where is QT_USE_IMPORTED_TARGETS not safe to use?

Alexander Neundorf a.neundorf-work at gmx.net
Wed Jun 22 12:05:28 EDT 2011


On Wednesday 22 June 2011, Stephen Kelly wrote:
> Wups, sent a moment to early...
> 
> Stephen Kelly wrote:
> > Clinton Stimpson wrote:
> >> It means the target must be created again with something like
> >> add_library(Qt4::QtCore UNKNOWN IMPORTED)
> >> 
> >> If GrantleeConfig.cmake were to do this:
> >> SET(QT_USE_IMPORTED_TARGETS 1)
> >> find_package(Qt4 REQUIRED)
> >> 
> >> then the imported targets would satisfy the Qt4::QtCore reference and
> >> others from your GrantleeTargets-debug.
> >> 
> >>> > The GrantleeConfig.cmake could set QT_USE_IMPORTED_TARGETS and a
> >>> > find_package(Qt4) to recreated those imported targets.
> >>> 
> >>> Ok, so maybe setting cis the thing that needs to
> >>> be done 'again' by project B?
> >> 
> >> Yes, or preferably in your GrantleeConfig.cmake so project B doesn't
> >> have to do it.
> > 
> > I tried creating the error scenario, but it does work even if the
> > downstream does not set QT_USE_IMPORTED_TARGETS and even if I don't put
> > it in GrantleeConfig either. Qt4::QtCore is never interpreted as a file
> > path because FindQt4.cmake sets it unconditionally, and only
> > conditionally sets QT_QTCORE_LIBRARY to Qt4::QtCore based on
> > QT_USE_IMPORTED_TARGETS.
> 
> That means that if GrantleeTarget-debug uses
> 
> IMPORTED_LINK_INTERFACE_LIBRARIES_DEBUG "Qt4::QtCore"

The statement above has the effect than when a user of Grantlee in a his 
project named Foo links against the Grantlee library, it will additionally 
link against "Qt4::QtCore".

If there is no target (imported or "normal", this doesn't matter) with that 
name in the project Foo, then "Qt4::QtCore" is considered a direct parameter 
for the linker and cmake will append (under Linux) a "-lQt4::QtCore to the 
linker command line.
Which would be wrong.

So for that to work correctly, an imported target with the name "Qt4::QtCore" 
must be created somewhere in the project Foo.
Typically by FindQt4, which should then create these imported targets.

You can still test in e.g. GrantleeConfig.cmake whether the target Qt4::QtCore 
exists and fail with FATAL_ERROR if it doesn't.

I strongly recommend using these imported targets, one reason is because 
otherwise when installing a binary package of Grantlee under Windows, it will 
not be possible to freely choose the install location, or better the location 
where Qt must be is then already hardcoded in GrantLeeConfig.cmake (because 
then there won't be "Qt4::QtCore" in GrantleeConfig.cmake, but something like 
"c:/where/was/Qt/at/buildtime/QtCore.dll"). When then linking against 
Grantleee, and Qt is installed somewhere else, it will fail.

Does this make the comment below more clear ?

Alex


More information about the CMake mailing list