CMake configuration specific library name when using multi-config generators


CMake configuration specific library name when using multi-config generators



Running cmake -G "Visual Studio 15 2017 Win64" on


cmake -G "Visual Studio 15 2017 Win64"


cmake_minimum_required(VERSION 3.10)
project(test CXX)
add_library(MyLib SHARED foo.cpp)
install(TARGETS MyLib DESTINATION $ENV{TEMP})



generates a project with several configurations (Release, Debug, ...) which can then be built with Visual Studio.


Release


Debug



The problem: When installing the library (i.e. building the generated project named INSTALL), a Debug build will happily overwrite a Release build and vice versa.


INSTALL


Debug


Release



I am looking for a means to create a different library name for each configuration, e.g. a MyLib for the release build and MyLibd for a debug build.


MyLib


MyLibd



What I have tried: When using a single-config generator, I would have queried CMAKE_BUILD_TYPE and provided a different name for the library for each configuration. However "Visual Studio 15 2017 Win64" is a multi-config generator and thus I cannot apply such a technique.


CMAKE_BUILD_TYPE


"Visual Studio 15 2017 Win64"



I have read about generator expressions but all examples show only generator expressions applied to input arguments, such as input files or directories. And indeed, something like


add_library(MyLib$<$<CONFIG:Debug>:d> SHARED foo.cpp)



will only produce nasty CMake error messages and does not yield the desired effect of having the library name depend on the configuration.



I could of course create a custom command which renames the file while installing, but in my understanding of CMake having "batteries included", there must be a simpler way. Generator expressions looked very promising, but it seems that doesn't work the way I have tried.



Question: How can I make the name of the library depend on the configuration?




2 Answers
2



You may assign DEBUG_POSTFIX property of the library target for have different postfix for debug builds.



Or you may do that for all targets by assigning CMAKE_DEBUG_POSTFIX variable:


set(CMAKE_DEBUG_POSTFIX "d")
#... Create a library after the setting the postfix.
add_library(MyLib SHARED foo.cpp)





set(CMAKE_CONFIG_POSTFIX "d") didn't have any effect. set_target_properties(MyLib PROPERTIES DEBUG_POSTFIX "d") works. Following the link you provided, I saw that the "all targets" setting should be CMAKE_<CONFIG>_POSTFIX. I.e. in my case: set(CMAKE_DEBUG_POSTFIX "d"). Both do exactly what I was looking for. Thank you.
– Adrian W
Jul 1 at 22:08


set(CMAKE_CONFIG_POSTFIX "d")


set_target_properties(MyLib PROPERTIES DEBUG_POSTFIX "d")


CMAKE_<CONFIG>_POSTFIX


set(CMAKE_DEBUG_POSTFIX "d")





Oops, I meant DEBUG instead of CONFIG. Fixed now.
– Tsyvarev
Jul 1 at 22:28



@Tsyvarev's hint on setting a property of the library target led to yet another solution: there is a target property OUTPUT_NAME and this does allow the use of generator expressions.



So, one can write


set_target_properties(MyLib PROPERTIES OUTPUT_NAME "MyLib$<$<CONFIG:Debug>:d>")





I posted this answer because it allows a freely configurable library name as I have originally asked for. However, @Tsyvarev's answer has some other advantages: it is syntactically simpler, it is easier to see the intent, which is indeed only to add a suffix to the name, and one has the possibility to add a suffix for all targets. Therefore Tsyvarev's answer is the accepted answer.
– Adrian W
Jul 2 at 11:18






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to add background colour in existing image using Swift?

Moria Casán

How to make file upload 'Required' in Contact Form 7?