[CMake] How to findXXX libraries with debug suffix

Alexander Neundorf a.neundorf-work at gmx.net
Sun Feb 14 12:07:25 EST 2010


On Tuesday 09 February 2010, Mike Jackson wrote:
> Here is one I wrote for Expat:
>
>
> ------8<------------------------------------------------
> # - Find expat
> # Find the native EXPAT headers and libraries.
> #
> #  EXPAT_INCLUDE_DIRS - where to find expat.h, etc.
> #  EXPAT_LIBRARIES    - List of libraries when using expat.
> #  EXPAT_LIBRARY_DEBUG - Debug version of Library
> #  EXPAT_LIBRARY_RELEASE - Release Version of Library
> #  EXPAT_FOUND        - True if expat found.
>
>
> ############################################
> #
> # Check the existence of the libraries.
> #
> ############################################
> # This macro was taken directly from the FindQt4.cmake file that is
> included # with the CMake distribution. This is NOT my work. All work was
> done by the # original authors of the FindQt4.cmake file. Only minor
> modifications were # made to remove references to Qt and make this file
> more generally applicable
> #########################################################################
>
> MACRO (ADJUST_LIB_VARS basename)
>   IF (${basename}_INCLUDE_DIR)
>
>   # if only the release version was found, set the debug variable also
> to the release version
>   IF (${basename}_LIBRARY_RELEASE AND NOT ${basename}_LIBRARY_DEBUG)
>     SET(${basename}_LIBRARY_DEBUG ${${basename}_LIBRARY_RELEASE})
>     SET(${basename}_LIBRARY       ${${basename}_LIBRARY_RELEASE})
>     SET(${basename}_LIBRARIES     ${${basename}_LIBRARY_RELEASE})
>   ENDIF (${basename}_LIBRARY_RELEASE AND NOT ${basename}_LIBRARY_DEBUG)



Yes, that's what FindQt4.cmake was doing basically, but we switched that now 
to use imported targets instead:

IF (QT_${basename}_LIBRARY_RELEASE OR QT_${basename}_LIBRARY_DEBUG)

  IF(NOT TARGET Qt4::${_camelCaseBasename})
    ADD_LIBRARY(Qt4::${_camelCaseBasename} UNKNOWN IMPORTED )

    IF (QT_${basename}_LIBRARY_RELEASE)
      SET_PROPERTY(TARGET Qt4::${_camelCaseBasename}  APPEND PROPERTY
                   IMPORTED_CONFIGURATIONS RELEASE)
       SET_PROPERTY(TARGET Qt4::${_camelCaseBasename}  PROPERTY
               IMPORTED_LOCATION_RELEASE "${QT_${basename}_LIBRARY_RELEASE}" )
    ENDIF (QT_${basename}_LIBRARY_RELEASE)

    IF (QT_${basename}_LIBRARY_DEBUG)
      SET_PROPERTY(TARGET Qt4::${_camelCaseBasename} APPEND PROPERTY
                   IMPORTED_CONFIGURATIONS DEBUG)
      SET_PROPERTY(TARGET Qt4::${_camelCaseBasename} PROPERTY
                   IMPORTED_LOCATION_DEBUG "${QT_${basename}_LIBRARY_DEBUG}" )
    ENDIF (QT_${basename}_LIBRARY_DEBUG)
  ENDIF(NOT TARGET Qt4::${_camelCaseBasename})

  SET(QT_${basename}_LIBRARY     Qt4::${_camelCaseBasename} )
  SET(QT_${basename}_LIBRARIES   Qt4::${_camelCaseBasename} )


Now you can either use the imported targets Qt4::QtCore etc. directly in your 
target_link_libraries() calls, or you can still use QT4_QTCORE_LIBRARIES, but 
they now point to the imported targets.

The advantage of these imported targets is that the different build 
configurations can be handled cleanly. I.e. you can specifiy a location for 
each build type, and you can map buildtypes from your project to those of the 
imported target. 
So I would recommend to try to use imported targets for this.

Alex


More information about the CMake mailing list