cmake:添加"d"后缀用于调试静态库 [英] cmake: add "d" suffix for debug build of static library

查看:826
本文介绍了cmake:添加"d"后缀用于调试静态库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为库实现一个类似于此处提到的库的命名方案: x32与x64的库名称

已设置CMakeLists.txt文件以创建静态库

add_library(test test.h test.cpp)

从cmake列表中创建Visual Studio解决方案后,将项目设置为将调试库test.lib写入/x64/Debug/test.lib,将发行版本写入/x64/Release/test.lib.我希望将它们都写到/lib/,但在调试版本后附加一个"d".这个想法是要得到

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

,如果可能的话,还可以有一个附加后缀,以获取64位版本

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

有直接的方法吗?


以后可以使用像这样的库在项目中很好地使用它: 为调试和发布版本链接不同的库在Windows的Cmake中?


我在从输出中删除调试"和发布"文件夹时遇到问题,可以通过以下答案解决:解决方案

CMAKE_DEBUG_POSTFIX 用于为调试库附加d:

set(CMAKE_DEBUG_POSTFIX d)

如果您不想全局设置,也可以使用

或者,如果您想在不同的体系结构上使用相同的目标名称,请设置一个变量,例如 CMAKE_STATIC_LIBRARY_SUFFIX (一堆,所以您可以为目标类型选择正确的类型,并根据要向其添加后缀的输出文件来选择).

由于您还提到了这个答案以查找此类库:首选使用导入的目标,而不是target_link_libraries的粗粒度旧版debugoptimized限定词. 配置文件包提供了一种方便的方式将此类导入的目标暴露给您的客户,并且它们还可以自动为您处理任何后缀恶作剧.

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?

解决方案

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.

这篇关于cmake:添加"d"后缀用于调试静态库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆