12

I would like to implement a naming scheme for libraries similar to the one mentioned here: Library name for x32 vs x64

The CMakeLists.txt file is setup to create a static library

add_library(test test.h test.cpp)

After creating a visual studio solution from the cmake lists the project is set up in such a way that the debug library test.lib is written to /x64/Debug/test.lib and the release version is written to /x64/Release/test.lib. I would prefer to write them both to /lib/ but append a "d" to the debug version. The idea is to get

/lib/test.lib
/lib/testd.lib

and if possible have an additional suffix for 64 bit builds to get

/lib/test.lib
/lib/test64.lib
/lib/testd.lib
/lib/test64d.lib

Is there a straightforward way to do this?


Edit: this can be used later nicely in the project using the libs like this: Linking different libraries for Debug and Release builds in Cmake on windows?


Edit: I had problems removing the Debug and Release folders from the output, which can be fixed by this answer: How to not add Release or Debug to output path?

Beginner
  • 5,277
  • 6
  • 34
  • 71
  • 1
    I haven't used these actually, but I think `CMAKE_STATIC_LIBRARY_PREFIX` and `CMAKE_STATIC_LIBRARY_SUFFIX` are intended for this purpose. – Marius Bancila Apr 05 '18 at 12:33
  • 1
    @MariusBancila this is the suffix to use for the end of a static library filename, .lib on Windows. I'm not sure it is intended to be changed to d.lib. – Beginner Apr 05 '18 at 12:50
  • Do have in mind that for anyone wanting to use your library this would immediately introduce more work. The user of your library will have to handle all the prefix and suffix spaghetti you are adding to your library's name. Even for a single library this is a pain to deal with let alone if you have to tackle this for several. Always try to offer an abstraction layer (e.g. `FindTest.cmake` in your case) that does the job automatically. – rbaleksandar Sep 13 '21 at 12:50

1 Answers1

23

CMAKE_DEBUG_POSTFIX is used for appending the d for debug libraries:

set(CMAKE_DEBUG_POSTFIX d)

If you do not want to set this globally, you can also use the DEBUG_POSTFIX target property instead on selected libraries.

There is no corresponding feature for distinguishing 32/64 bit builds, but since it is impossible to mix those two in the same CMake configuration, you can easily distinguish those cases manually, e.g.

if(CMAKE_SIZEOF_VOID_P EQUAL 4)
    set(ARCH_POSTFIX "")
else()
    set(ARCH_POSTFIX 64)
endif()

add_library(my_lib${ARCH_POSTFIX} [...])

Or, if you want to use the same target name on the different architectures, set a variable like CMAKE_STATIC_LIBRARY_SUFFIX (there exist a whole bunch of them, so you can select the correct one for your target type and based on which output files you want to append a suffix to).

And since you also mentioned this answer for finding such libraries: Prefer using imported targets instead of the coarse-grained legacy debug and optimized qualifiers for target_link_libraries. Config file packages provide a convenient way of exposing such imported targets to your clients, and they also handle any suffix shenanigans automatically for you.

ComicSansMS
  • 51,484
  • 14
  • 155
  • 166
  • Which of this solutions are "Modern cmake"? I now always hesitate to use a direct `set()` because I want to know if there is a dedicated solution instead. – Sandburg May 28 '19 at 09:35
  • 3
    @Sandburg The modern approach would be using the `DEBUG_POSTFIX` target property on the providing side and have a config file package generated for downstream users of the library. – ComicSansMS May 28 '19 at 11:45
  • 5
    @ComicSansMS That's exactly right. For future readers this is how it might look: `set_target_properties( PROPERTIES DEBUG_POSTFIX "d")` – Tom Apr 26 '20 at 18:56