[CMake] Configuration dependent link

Stephan Menzel stephan.menzel at gmail.com
Tue Nov 10 10:26:06 EST 2015


Hello,

I am trying to have config dependent link. Basically, coming from a set of
3rd party location scripts I have a Visual Studio target which I would like
to equip with imported libs rather than the existing "manual" scripts. With
that I have a few trouble that maybe someone can help me with.

Configuration types are

set (CMAKE_CONFIGURATION_TYPES "Debug;Release;RelWithDebInfo" CACHE STRING
"..." FORCE)

Libs usually come in 2 flavors, Release and Debug.

So I create imported libs and put the configurations in:

 ... create imported lib
 set_property(TARGET ${name} PROPERTY IMPORTED_CONFIGURATIONS "")
 set_property(TARGET ${name} APPEND PROPERTY IMPORTED_CONFIGURATIONS
"Debug")
 set_property(TARGET ${name} APPEND PROPERTY IMPORTED_CONFIGURATIONS
"Release")
 set_property(TARGET ${name} APPEND PROPERTY IMPORTED_CONFIGURATIONS
"RelWithDebInfo")

So my first question, what is the relation here? Do I have to do this? Is
the configuration name case sensitive?

So then I worked my way through creating the imported libs and augmented
the scripts in order to put locations and dependencies in the target's
properties:

set_property(TARGET ${name} APPEND PROPERTY IMPORTED_IMPLIB_Release
${implib})
set_property(TARGET ${name} APPEND PROPERTY IMPORTED_IMPLIB_RelWithDebInfo
${implib})
set_property(TARGET ${name} APPEND PROPERTY IMPORTED_IMPLIB_Debug ${implib})
...and same for locations (DLLs)

"implib" is the configuration specific lib here. After the procedure the
property  IMPORTED_IMPLIB_Debug and so on contain all the libs.

When all the imported libs are created I was hoping to be able to link
against them like so:

target_link_libraries(${mylib} PUBLIC ${someimportedlib})

My problem is: Which Config is chosen? How can I tell target_link_libraries
to link ${mylib} in Config Debug against ${someimportedlib} with the
IMPORTED_IMPLIB_Debug properties? I was hoping this would be automatic as
it is obvious information but that doesn't seem to work. Instead I usually
get an additional linker dependency named my::lib.obj-NOTFOUND

Also when doing all this manually appending to the property multiple times
(as for example boost or Qt libraries with lots of libs to link against)
seems to cause errors eventually. They appear in Visual Studio's properties
dialog as _one_ Linker Dependency. For example:

Like I add (resolved)

add_library(sys::boost SHARED GLOBAL)
... setup lib ...
set_property(TARGET sys::boost APPEND PROPERTY IMPORTED_IMPLIB_Release
    D:\3rd_party\boost_1_59_0\lib\vc110\x64\boost_atomic-vc110-mt-1_59.lib)
set_property(TARGET sys::boost APPEND PROPERTY IMPORTED_IMPLIB_Release
    D:\3rd_party\boost_1_59_0\lib\vc110\x64\boost_chrono-vc110-mt-1_59.lib)
set_property(TARGET sys::boost APPEND PROPERTY IMPORTED_IMPLIB_Release

D:\3rd_party\boost_1_59_0\lib\vc110\x64\boost_date_time-vc110-mt-1_59.lib)
... and so on and the same for corresponding DLLs as location...

eventually yields _one_ linker dependency called:

D:\3rd_party\boost_1_59_0\lib\vc110\x64\boost_chrono-vc110-mt-1_59.lib%3BE:\3rd_party\boost_1_59_0\lib\vc110\x64\boost_date_time-vc110-mt-1_59.lib%3BE:\3rd_party\boost_1_59_0\lib\vc110\x64\boost_filesystem-vc110-mt-1_59.lib%3BE:\3rd_party\boost_1_59_0\lib\vc110
.... and so on.

Visual Studio seems to hate that and throws errors. So I thought about
extracting this property, foreach() it and then add the tokens as linker
dependencies one at a time. Can I do that somehow? Config specific? And if
so: why? When I dive this deep into what I would hope CMake is all about, I
might as well keep the old hand made script based system that doesn't use
imported targets.

Sooo.....

TL;DR

1) How can I have config specifc imported targets with multiple IMPLIBs?
2) Why should I? Isn't there a better, more canonical way? Hasn't this
wheel been invented hundreds of times?

Thanks a bunch for any hints here... I'm lost in properties.

Stephan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20151110/5293aab0/attachment-0001.html>


More information about the CMake mailing list