C++ 在 Windows 上的 Cmake 中为调试和发布构建链接不同的库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2209929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:33:22  来源:igfitidea点击:

Linking different libraries for Debug and Release builds in Cmake on windows?

c++windowscmake

提问by gct

So I've got a library I'm compiling and I need to link different third party things in depending on if it's the debug or release build (specifically the release or debug versions of those libraries). Is there an easy way to do this in Cmake?

所以我有一个正在编译的库,我需要根据它是调试版本还是发布版本(特别是这些库的发布版本或调试版本)来链接不同的第三方内容。在 Cmake 中是否有一种简单的方法可以做到这一点?

Edit: I should note I'm using visual studio

编辑:我应该注意我正在使用 Visual Studio

回答by Mike Willekes

According to the CMake documentation:

根据CMake 文档

target_link_libraries(<target> [lib1 [lib2 [...]]] [[debug|optimized|general] <lib>] ...)

A "debug", "optimized", or "general" keyword indicates that the library immediately following it is to be used only for the corresponding build configuration.

“debug”、“optimized”或“general”关键字表示紧随其后的库仅用于相应的构建配置。

So you should be able to do this:

所以你应该能够做到这一点:

add_executable( MyEXE ${SOURCES})

target_link_libraries( MyEXE debug 3PDebugLib)
target_link_libraries( MyEXE optimized 3PReleaseLib)

回答by Tarc

Somehow the answer from @Mike Willekes got CMake linking in the same target both release and debugfor me :(

不知何故,@Mike Willekes 的答案让 CMake 链接到了同一个目标为我发布和调试:(

I only got this working by setting both configurations in one line, as suggested by @sakra in a related question- and doing so for every library that needed to be linked:

正如@sakra在相关问题中所建议的那样我只是通过在一行中设置两个配置来实现此目的- 并且为每个需要链接的库执行此操作:

target_link_libraries ( app
    debug ${Boost_FILESYSTEM_LIBRARY_DEBUG}
    optimized ${Boost_FILESYSTEM_LIBRARY_RELEASE} )

target_link_libraries ( app
    debug ${Boost_LOG_LIBRARY_DEBUG}
    optimized ${Boost_LOG_LIBRARY_RELEASE} )

target_link_libraries ( app
    debug ${Boost_PROGRAM_OPTIONS_LIBRARY_DEBUG}
    optimized ${Boost_PROGRAM_OPTIONS_LIBRARY_RELEASE} )

# ...

回答by akaltar

I would like to add a few notes to the previous answers.

我想在以前的答案中添加一些注释。

If you need to create a list of multiple files you want to link and store that in a cache variable then you need to add the optimizedor debugspecified before each and every library. This can be especially useful for larger makefiles/projects.

如果您需要创建要链接的多个文件的列表并将其存储在缓存变量中,那么您需要在每个库之前添加optimizeddebug指定。这对于较大的生成文件/项目特别有用。

So for example you could do something like this:

例如,您可以执行以下操作:

set( MyFavLib_LIBRARIES 
    debug debug/module1.lib optimized release/module1.lib
    debug debug/module2.lib optimized release/module2.lib )
target_link_libraries( app ${MyFavLib_LIBRARIES} )

回答by Konrad

What worked for me was to use $(Configuration)macro in a lib path provided to cmake.

对我有用的是$(Configuration)在提供给 cmake 的 lib 路径中使用宏。

So, assuming libs are stored in separate, correctly named folders, e.g.:

因此,假设库存储在单独的、正确命名的文件夹中,例如:

C:\boost\lib\Debug\libfoo.lib
C:\boost\lib\Release\libfoo.lib

You can then call cmake with:

然后,您可以使用以下命令调用 cmake:

cmake -G "Visual Studio 10 2010" -DBOOST_LIBRARYDIR=C:\boost\lib$(Configuration)\libfoo.lib

That'll generate .vcxproj with Additional Dependencies including C:\boost\lib\$(Configuration)\libfoo.lib, what is evaluated to either C:\boost\lib\Release\libfoo.libor C:\boost\lib\Debug\libfoo.libdepending on a chosen Configuration.

这将生成具有附加依赖项的 .vcxproj,包括C:\boost\lib\$(Configuration)\libfoo.lib评估为C:\boost\lib\Release\libfoo.libC:\boost\lib\Debug\libfoo.lib取决于所选配置的内容。