Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cmake: add "d" suffix for debug build of static library

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?

like image 706
Beginner Avatar asked Apr 05 '18 12:04

Beginner


People also ask

How do I add a library path to CMake?

To add a library in CMake, use the add_library() command and specify which source files should make up the library. Rather than placing all of the source files in one directory, we can organize our project with one or more subdirectories.

What is target_ link_ libraries in CMake?

target_link_libraries is responsible for adding a library into the linker's command line. If you use some library but doesn't specify it for the linker, you will got an "undefined reference" (or an "unresolved externals") error when create an executable or a shared library: stackoverflow.com/questions/12573816/…

Does CMake default to debug or release?

It will now default to using a debug build if the source directory is a git clone, or a release build if not. It is also quite easy to customize its behavior according to the preferences of your project.


1 Answers

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.

like image 153
ComicSansMS Avatar answered Oct 19 '22 03:10

ComicSansMS