与 CMAKE 链接的不同调试和发布库

发布于 2024-10-27 18:42:08 字数 393 浏览 2 评论 0原文

已经有一个线程,但并没有真正的帮助。我希望能够链接例如 Foo.lib 用于 Release 配置和 Foo_d.lib 用于 Debug 配置,我怎样才能做到这一点? 如果我这样做:

target_link_libraries(MyEXE debug Foo_d)
target_link_libraries(MyEXE optimized Foo)

那么我的项目中有两个库用于调试配置? 为什么没有发布选项? (我正在使用 Visual Studio)。

There was already a Thread which did not help really. I want to be able to link for example Foo.lib for Release Config and Foo_d.lib for Debug Config , how can I achieve this?
If I do this:

target_link_libraries(MyEXE debug Foo_d)
target_link_libraries(MyEXE optimized Foo)

then I have both libraries in my project for the debug config?
Why is there no Release option? (I am using Visual Studio).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

画离情绘悲伤 2024-11-03 18:42:08

target_link_libraries 需要一个列表,您不需要调用它两次。以下内容将起作用:

target_link_libraries(MyEXE debug Foo_d optimized Foo)

要回答另一个答案的评论中提出的问题,使用多个库的工作方式如下:

target_link_libraries(MyEXE
    debug Foo1_d optimized Foo1
    debug Foo2_d optimized Foo2)

请注意,如果您还将库构建为 CMake 项目的一部分,则无需指定 debug 或优化。 CMake 将为您选择正确的一个。

target_link_libraries takes a list, you don't need to call it twice. The following will work:

target_link_libraries(MyEXE debug Foo_d optimized Foo)

And to answer a question asked in the comments of another answer, working with multiple libraries works like so:

target_link_libraries(MyEXE
    debug Foo1_d optimized Foo1
    debug Foo2_d optimized Foo2)

Note that if you also build the library as part of the CMake project, you don't need to specify debug or optimized. CMake will choose the right one for you.

感情旳空白 2024-11-03 18:42:08

解决办法是:

SET(LINK_LIBRARY optimized Foo debug Foo_d)
target_link_libraries(MyEXE ${LINK_LIBRARY})

The solution is:

SET(LINK_LIBRARY optimized Foo debug Foo_d)
target_link_libraries(MyEXE ${LINK_LIBRARY})
九命猫 2024-11-03 18:42:08

当您的库是项目的一部分或者您是项目的一部分时,就没有问题
使用 find_package 命令的配置模式导入它(请参阅文档示例)。
如果您无法修改第 3 方,那么它将生成 Config.cmake
(它可能不使用cmake工具或者你不想这样做)答案是模拟
这样的过程:

add_library(foo STATIC IMPORTED)
set_target_properties(foo PROPERTIES IMPORTED_LOCATION_DEBUG "/path/to/foo-d.lib")
set_target_properties(foo PROPERTIES IMPORTED_LOCATION_RELEASE "/path/to/foo.lib")

target_link_libraries(MyEXE foo)

请注意,与“调试”/“优化”功能不同,这种方法不仅限于调试/发布配置:

set_target_properties(foo PROPERTIES IMPORTED_LOCATION_MINSIZEREL "/path/to/foo-small.lib")

您还可以获得一些好东西,例如 INTERFACE_INCLUDE_DIRECTORIES

set_target_properties(foo PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "/path/to/foo/includes")

include_directories("/path/to/foo/includes") # this line not needed
target_link_libraries(MyEXE foo) # this command will add "/path/to/foo/includes" for you

和传递链接:

add_library(boo STATIC IMPORTED)
set_target_properties(boo PROPERTIES IMPORTED_LOCATION_DEBUG "/path/to/boo-d.lib")
set_target_properties(boo PROPERTIES IMPORTED_LOCATION_RELEASE "/path/to/boo.lib")

add_library(foo STATIC IMPORTED)
set_target_properties(foo PROPERTIES IMPORTED_LOCATION_DEBUG "/path/to/foo-d.lib")
set_target_properties(foo PROPERTIES IMPORTED_LOCATION_RELEASE "/path/to/foo.lib")

set_target_properties(foo PROPERTIES INTERFACE_LINK_LIBRARIES boo) # foo depends on boo

target_link_libraries(MyEXE foo) # boo will be linked automatically

当然,您可以使用常规的 cmake 命令,例如 find_libraryfind_package (... MODULE) 来估计位置而不是对其进行硬编码。

There is no problems when your library is a part of the project or you're
importing it using config mode of find_package command (see documentation and example).
In case you can't modify 3rd party so it will produce <package>Config.cmake
(it may not use cmake tool or you don't want to do it) the answer is to emulate
such process:

add_library(foo STATIC IMPORTED)
set_target_properties(foo PROPERTIES IMPORTED_LOCATION_DEBUG "/path/to/foo-d.lib")
set_target_properties(foo PROPERTIES IMPORTED_LOCATION_RELEASE "/path/to/foo.lib")

target_link_libraries(MyEXE foo)

note that unlike "debug"/"optimized" feature such approach is not limited to Debug/Release configs:

set_target_properties(foo PROPERTIES IMPORTED_LOCATION_MINSIZEREL "/path/to/foo-small.lib")

also you've got some goodies like INTERFACE_INCLUDE_DIRECTORIES:

set_target_properties(foo PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "/path/to/foo/includes")

include_directories("/path/to/foo/includes") # this line not needed
target_link_libraries(MyEXE foo) # this command will add "/path/to/foo/includes" for you

and transitive linking:

add_library(boo STATIC IMPORTED)
set_target_properties(boo PROPERTIES IMPORTED_LOCATION_DEBUG "/path/to/boo-d.lib")
set_target_properties(boo PROPERTIES IMPORTED_LOCATION_RELEASE "/path/to/boo.lib")

add_library(foo STATIC IMPORTED)
set_target_properties(foo PROPERTIES IMPORTED_LOCATION_DEBUG "/path/to/foo-d.lib")
set_target_properties(foo PROPERTIES IMPORTED_LOCATION_RELEASE "/path/to/foo.lib")

set_target_properties(foo PROPERTIES INTERFACE_LINK_LIBRARIES boo) # foo depends on boo

target_link_libraries(MyEXE foo) # boo will be linked automatically

Of course you can use regular cmake commands like find_library and find_package(... MODULE) to estimate locations instead of hardcoding them.

谜兔 2024-11-03 18:42:08

如果您的调试/发布库遵循某种模式,例如调试库上的 _d,您可以避免重复:

set (MY_LIBS
    foo
    bar
    baz
)

# Generate the list of files to link, per flavor.
set (LINK_LIST "")
foreach(x ${MY_LIBS})
    list (APPEND LINK_LIST debug ${x}_d optimized ${x})
endforeach()

target_link_libraries (mytarget
    commonlib1
    commonlib2
    ${LINK_LIST}
    )

这将生成

debug foo_d optimized foo
debug bar_d optimized bar

target_link_libraries 期望的相应行。

If you have debug/release libs that follow a certain pattern, like _d on the debug ones, you can avoid repeating yourself with:

set (MY_LIBS
    foo
    bar
    baz
)

# Generate the list of files to link, per flavor.
set (LINK_LIST "")
foreach(x ${MY_LIBS})
    list (APPEND LINK_LIST debug ${x}_d optimized ${x})
endforeach()

target_link_libraries (mytarget
    commonlib1
    commonlib2
    ${LINK_LIST}
    )

This will generate the appropriate

debug foo_d optimized foo
debug bar_d optimized bar

lines that target_link_libraries expects.

昵称有卵用 2024-11-03 18:42:08

使用带有调试和优化参数的 target_link_libraries 的上部答案不适用于我的 cmake 3.14.6。
如果您遇到此错误,请尝试使用以下类型的代码:

set (FreeImage debug "FreeImageD" optimized "FreeImage")
set (Glog debug "glogd" optimized "glog")
set (EXTRA_LIBS ${FreeImage} ${Glog})
target_link_libraries(TexCompressor ${EXTRA_LIBS})

它只是将调试/优化对更改为变量定义。它适用于我的版本。

The upper answer of using target_link_libraries with debug and optimized param not working for my cmake 3.14.6.
If you encounter this bug, try using the following type of code:

set (FreeImage debug "FreeImageD" optimized "FreeImage")
set (Glog debug "glogd" optimized "glog")
set (EXTRA_LIBS ${FreeImage} ${Glog})
target_link_libraries(TexCompressor ${EXTRA_LIBS})

It just change the debug/optimized pair to variable definition. It works for my version.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文