[CMake] How to add a target link library, but only for a custom configuration ?
Michael Hertling
mhertling at online.de
Thu May 5 20:12:40 EDT 2011
On 05/05/2011 12:51 PM, Glenn Coombs wrote:
> Thanks for that link Michael. It doesn't please me, but I can confirm that
> it does work :-) It's a nice hack around a deficiency in cmake. I ended up
> using this code:
>
> # First create a dummy library to hang the pthreads
> # dependency on via the IMPORTED_LINK_INTERFACE_LIBRARIES property.
> file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/pthreads_dummy.c "")
> add_library(pthreads_dummy STATIC
> ${CMAKE_CURRENT_BINARY_DIR}/pthreads_dummy.c)
>
> # make sure the dummy library gets built
> add_dependencies(systemc pthreads_dummy)
>
> # export the dummy library so we can include it as an imported target
> export(TARGETS pthreads_dummy NAMESPACE imported FILE
> importedpthreads_dummy.cmake)
> include(${CMAKE_CURRENT_BINARY_DIR}/importedpthreads_dummy.cmake)
>
> # use the imported target to add pthread dependency for the DEBUGPTHREADS
> config
> set_target_properties(importedpthreads_dummy
> PROPERTIES
> IMPORTED_LINK_INTERFACE_LIBRARIES_DEBUGPTHREADS ${PTHREADS_LIBRARIES})
>
> target_link_libraries(myLib importedpthreads_dummy)
>
> which is a lot of extra lines when all I really want is something like this:
>
> target_link_libraries(myLib CONFIG DebugPthreads pthreads)
Yes, absolutely. Although setups with libraries which are needed in some
configurations only are quite rare, AFAIK, your case shows that this may
well happen. ;-) Possible - and more appropriate - solutions could be:
- New target properties, say, EXTRA_LINK_LIBRARIES[_<CONFIG>] that
are just added to the respective targets' link command lines.
- Permission of empty or absent IMPORTED_LOCATION[_<CONFIG>]
properties; with these, you might just have said
ADD_LIBRARY(pthreads SHARED IMPORTED)
SET_TARGET_PROPERTIES(pthreads PROPERTIES
IMPORTED_LOCATION_DEBUGPTHREADS ${PTHREADS_LIBRARIES})
TARGET_LINK_LIBRARIES(myLib pthreads)
and that's it. Perhaps, this would be worth a feature request.
Regards,
Michael
> On 26 April 2011 15:15, Michael Hertling <mhertling at online.de> wrote:
>
>>
>> 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