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

Glenn Coombs glenn.coombs at gmail.com
Tue Apr 26 09:40:38 EDT 2011


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20110426/5a20bbbf/attachment.htm>


More information about the CMake mailing list