[CMake] How to add a target link library, but only for a custom configuration ?

Michael Hertling mhertling at online.de
Tue Apr 26 10:15:35 EDT 2011


On 04/26/2011 03:40 PM, Glenn Coombs wrote:
> I am using cmake 2.8.2 and I have added a custom configuration to my project
> like this:
> 
> # Add configuration for debug pthreads builds based on the debug
> configuration
> #
> =================================================================================
> set(CMAKE_C_FLAGS_DEBUGPTHREADS                    ${CMAKE_C_FLAGS_DEBUG}
>             CACHE STRING "Flags used by the compiler during DebugPthreads
> builds")
> set(CMAKE_CXX_FLAGS_DEBUGPTHREADS                ${CMAKE_CXX_FLAGS_DEBUG}
>         CACHE STRING "Flags used by the compiler during DebugPthreads
> builds")
> set(CMAKE_EXE_LINKER_FLAGS_DEBUGPTHREADS
> ${CMAKE_EXE_LINKER_FLAGS_DEBUG}        CACHE STRING "Flags used by the
> linker for executables during DebugPthreads builds")
> set(CMAKE_SHARED_LINKER_FLAGS_DEBUGPTHREADS
> ${CMAKE_SHARED_LINKER_FLAGS_DEBUG}    CACHE STRING "Flags used by the linker
> for shared libraries during DebugPthreads builds")
> set(CMAKE_MODULE_LINKER_FLAGS_DEBUGPTHREADS
> ${CMAKE_MODULE_LINKER_FLAGS_DEBUG}    CACHE STRING "Flags used by the linker
> for loadable modules during DebugPthreads builds")
> 
> # add in the details specific to this configuration
> set(CMAKE_C_FLAGS_DEBUGPTHREADS            "${CMAKE_C_FLAGS_DEBUGPTHREADS}
> /DSC_USE_PTHREADS")
> set(CMAKE_CXX_FLAGS_DEBUGPTHREADS        "${CMAKE_CXX_FLAGS_DEBUGPTHREADS}
> /DSC_USE_PTHREADS")
> 
> mark_as_advanced(
>     CMAKE_C_FLAGS_DEBUGPTHREADS
>     CMAKE_CXX_FLAGS_DEBUGPTHREADS
>     CMAKE_EXE_LINKER_FLAGS_DEBUGPTHREADS
>     CMAKE_SHARED_LINKER_FLAGS_DEBUGPTHREADS
>     CMAKE_MODULE_LINKER_FLAGS_DEBUGPTHREADS
> )
> 
> # This variable is only set for multi-config IDE generators like MSVC
> if(CMAKE_CONFIGURATION_TYPES)
>     list(APPEND CMAKE_CONFIGURATION_TYPES DebugPthreads)
>     list(REMOVE_DUPLICATES CMAKE_CONFIGURATION_TYPES)
>     set(CMAKE_CONFIGURATION_TYPES "${CMAKE_CONFIGURATION_TYPES}"
>         CACHE STRING "Semicolon separated list of supported configuration
> types [Debug|Release|MinSizeRel|RelWithDebInfo|DebugPthreads]"
>         FORCE)
> endif()
> 
> This works and inside Visual Studio 2008 I now get a DebugPthreads
> configuration in addition to the normal 4.  As part of my project I build a
> library which I now want to add a target dependency for, but only in the
> DebugPthreads configuration.  Something like this:
> 
> add_library(myLib ${sources})
> 
> if (activeConfig is DebugPthreads)
>     target_link_libraries(myLib ${FULL_PATH_TO_PTHREADS_LIB})
> endif()
> 
> There doesn't seem to be a way to specify target_link_libraries() for
> selected configurations.  I see that it can be specified for all debug or
> all release configurations but that isn't what I need.  The Debug and
> Release versions of myLib do not depend on the pthreads library but the
> DebugPthreads version of myLib does.
> 
> Googling for answers has revealed several people with the similar problems
> but no clear answers.  I have seen recommendations to use an imported
> library and the IMPORTED_LOCATION and IMPORTED_CONFIGURATIONS properties.  I
> tried this code:
> 
> add_library(my_pthreads STATIC IMPORTED)
> set_target_properties(my_pthreads PROPERTIES
>     IMPORTED_CONFIGURATIONS                "DebugPthreads"
>     IMPORTED_LOCATION_DEBUGPTHREADS        "${PTHREADS_LIBRARIES}"
>     )
> target_link_libraries(myLib my_pthreads)
> 
> but that includes the pthreads library as a target dependency in all
> configurations.  If I omit the IMPORTED_CONFIGURATIONS options then I get
> the correct result for the DebugPthreads configuration but all other
> configurations are trying to find a library called my_pthreads-NOTFOUND.
> 
> Is there a way to do what I want in cmake ?
> 
> --
> Glenn

There is a possibility with an intermediate empty static library which
gets reimported into the project and equipped with the target property
IMPORTED_LINK_INTERFACE_LIBRARIES_<CONFIG>, but I don't know if that
approach will please you... ;) Anyway, see [1] for the details.

Regards,

Michael

[1] http://www.mail-archive.com/cmake@cmake.org/msg34680.html

PS: Empty static libraries aren't allowed on Windows; use

FILE(WRITE ${CMAKE_BINARY_DIR}/dummy.c "")
ADD_LIBRARY(dummy STATIC dummy.c)

to satisfy the Visual Studio tools.


More information about the CMake mailing list