与 CMAKE 链接的不同调试和发布库
已经有一个线程,但并没有真正的帮助。我希望能够链接例如 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
target_link_libraries 需要一个列表,您不需要调用它两次。以下内容将起作用:
要回答另一个答案的评论中提出的问题,使用多个库的工作方式如下:
请注意,如果您还将库构建为 CMake 项目的一部分,则无需指定 debug 或优化。 CMake 将为您选择正确的一个。
target_link_libraries takes a list, you don't need to call it twice. The following will work:
And to answer a question asked in the comments of another answer, working with multiple libraries works like so:
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.
解决办法是:
The solution is:
当您的库是项目的一部分或者您是项目的一部分时,就没有问题Config.cmake
使用
find_package
命令的配置模式导入它(请参阅文档 和示例)。如果您无法修改第 3 方,那么它将生成
(它可能不使用cmake工具或者你不想这样做)答案是模拟
这样的过程:
请注意,与“调试”/“优化”功能不同,这种方法不仅限于调试/发布配置:
您还可以获得一些好东西,例如 INTERFACE_INCLUDE_DIRECTORIES:
和传递链接:
当然,您可以使用常规的 cmake 命令,例如
find_library
和find_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:
note that unlike "debug"/"optimized" feature such approach is not limited to Debug/Release configs:
also you've got some goodies like INTERFACE_INCLUDE_DIRECTORIES:
and transitive linking:
Of course you can use regular cmake commands like
find_library
andfind_package(... MODULE)
to estimate locations instead of hardcoding them.如果您的调试/发布库遵循某种模式,例如调试库上的 _d,您可以避免重复:
这将生成
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:
This will generate the appropriate
lines that
target_link_libraries
expects.使用带有调试和优化参数的 target_link_libraries 的上部答案不适用于我的 cmake 3.14.6。
如果您遇到此错误,请尝试使用以下类型的代码:
它只是将调试/优化对更改为变量定义。它适用于我的版本。
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:
It just change the debug/optimized pair to variable definition. It works for my version.