問題描述
已經有一個線程,但實際上并沒有幫助.我希望能夠鏈接例如 Foo.lib 用于 Release 配置和 Foo_d.lib 用于 Debug 配置,我怎樣才能做到這一點?如果我這樣做:
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?
非常感謝!
推薦答案
當你的庫是項目的一部分或者你是使用 find_package
命令的配置模式導入它(參見 文檔 和 example).如果你不能修改 3rd 方,它會產生
(它可能不使用 cmake 工具或者您不想這樣做)答案是模擬這樣的過程:
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")
你還有一些好東西,比如INTERFACE_INCLUDE_DIRECTORIES:
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
和傳遞鏈接:
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_library
和 find_package(... MODULE)
來估計位置,而不是對其進行硬編碼.
Of course you can use regular cmake commands like find_library
and find_package(... MODULE)
to estimate locations instead of hardcoding them.
這篇關于調試和發布庫鏈接與 CMAKE (VISUAL STUDIO)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!