Krazer
Krazer

Reputation: 515

CMake link to only release config of target in both debug and release

Is there another a way to link only the release lib of a target when including the target with target_link_libraries for both release and debug configs.

I know target_link_libraries has the options optimize and debug and that it can be done like this

target_link_libraries(current_target
    optimized $<TARGET_PROPERTY:lib_target,IMPORTED_IMPLIB_RELEASE>
    debug $<TARGET_PROPERTY:lib_target,IMPORTED_IMPLIB_RELEASE> 
)

However I generally keep the targets in a list

set(target_list
    lib_target1
    lib_target2
    ...
)

and I perform other things on the same list, like getting the binary directory of the target to include in the search path for debugging. Using the optimized and debug options also do not allow the lib_target... properties to be passed along through the current_target. I can work around it just curious if there is another way?

Upvotes: 4

Views: 1851

Answers (1)

Tsyvarev
Tsyvarev

Reputation: 66118

If you link with IMPORTED target, then its configuration-dependent properties refer to "imported configuration". You may always adjust mapping between configurations of your project and imported ones:

  1. Global configuration mapping is adjusted by CMAKE_MAP_IMPORTED_CONFIG_<CONFIG> variables.

    The setting below will use Release configuration of every IMPORTED target for any of Release, Debug or RelWithDebugInfo configurations of your project:

     set(CMAKE_MAP_IMPORTED_CONFIG_RELEASE Release)
     set(CMAKE_MAP_IMPORTED_CONFIG_DEBUG Release)
     set(CMAKE_MAP_IMPORTED_CONFIG_RELWITHDEBUGINFO Release)
    

Note, that these settings should be issued before creating of IMPORTED targets. That is, if such targets are created with find_package() calls, the settings should precede these calls.

  1. Per-target configuration mapping is adjusted by MAP_IMPORTED_CONFIG_<CONFIG> properties.

    Settings below do the same as global settings above but only for lib_target1 IMPORTED target:

     set_target_properties(lib_target1 PROPERTIES
         MAP_IMPORTED_CONFIG_RELEASE Release
         MAP_IMPORTED_CONFIG_DEBUG Release
         MAP_IMPORTED_CONFIG_RELWITHDEBUGINFO Release)
    

    These settings can be applied only after given IMPORTED target is created, e.g. after find_package() call.

It is worth to mention, that you may also specify fallback imported configurations:

set(CMAKE_MAP_IMPORTED_CONFIG_DEBUG Release Debug)

With such setting, if your project is built in Debug configuration, and some IMPORTED target doesn't have Release configuration, then its Debug configuration will be used. (But if that target has neither Release nor Debug configuration, CMake will emit an error).

Upvotes: 5

Related Questions