使用Visual Studio调试和发布的find_package [英] find_package for both debug and release with Visual Studio
问题描述
我想弄清楚如何在我的cmake项目中包含第三方库.目前,我正在构建Poco和其他一些软件包,它们都生成了与find_package一起使用的各自的Config.cmake.我有一个包装好的构建脚本,可以构建我的所有依赖关系,并将它们分别打包以进行调试和发布(我不想调整它们的cmake脚本,除非我真的非常需要维护,除非我真的需要这样做).
I'm tearing my hair out about how to include thrid party libraries in my cmake project. Currently I build Poco and a bunch of others that all generate their respective Config.cmake which I use with find_package. I have a wrapping build script that builds all of my dependencies and package them separately for debug and release (I don't want to tweak their cmake-scripts unless I really really really need to because maintanance).
我认为我可以做到:
find_package(Foo
HINTS "${CMAKE_SOURCE_DIR}/some/path/debug/libFoo/lib/cmake"
REQUIRED
)
get_target_property(LIB_FOO_DEBUG lib_foo LOCATION)
find_package(Foo
HINTS "${CMAKE_SOURCE_DIR}/some/path/release/libFoo/lib/cmake"
REQUIRED
)
get_target_property(LIB_FOO_RELEASE lib_foo LOCATION)
set(LIB_FOO_LIBRARIES optimized "${LIB_FOO_RELEASE}" debug "${LIB_FOO_DEBUG}")
message("LIB_FOO_LIBRARIES: \"${LIB_FOO_LIBRARIES}\"")
此产量:LIB_FOO_LIBRARIES: "optimized;C:/path/to/proj/some/path/debug/libFoo/lib/foo.lib;debug;C:/path/to/proj/some/path/debug/libFoo/lib/foo.lib"
似乎不希望第一次对目标Foo的find_package调用进行缓存.
It seems like the first call to find_package for target Foo is cached, whis I don't really want.
我会以错误的方式处理吗?如何通过Visual Studio生成器正确使用第三方库?
Am I going about this the wrong way? How do I properly work with third party libraries with the Visual Studio generator?
任何指针都将不胜感激.
Any pointers are greatly appreciated.
推荐答案
目标Foo对find_package的第一次调用已被缓存
the first call to find_package for target Foo is cached
是的.因此,您不能两次发出find_package
并获得不同的结果(除非第一次呼叫失败).
Yes. So you cannot issue find_package
twice and get different results (unless the first call failed).
这是第三方程序包,它负责multiconfig-usage,即应正确编写*Config.cmake
/Find*.cmake
文件. (例如,FindBoost.cmake
支持多配置用法).
It is third-party package who is responsible for multiconfig-usage, that is it should have properly written *Config.cmake
/Find*.cmake
file. (E.g., FindBoost.cmake
support multi-config usage).
否则,您应该为使用multiconfig方式使用软件包做一些技巧.
Otherwise, you should do some tricks for use package in multiconfig manner.
例如,如果您猜测配置之间的唯一区别是路径中的debug
/release
子字符串,则可以调用find_package()
进行调试安装,然后使用string(REPLACE)
来获取特定于发行版的路径:
E.g., if you guess that only difference between configurations is debug
/release
substrings in paths, you can call find_package()
for debug installation, then use string(REPLACE)
for get release-specific paths:
find_package(Foo
HINTS "${CMAKE_SOURCE_DIR}/some/path/debug/libFoo/lib/cmake"
REQUIRED
)
get_target_property(LIB_FOO_DEBUG lib_foo LOCATION)
string(REPLACE debug release LIB_FOO_RELEASE ${LIB_FOO_DEBUG})
# Use generator expressions, so variable can be used not only by target_link_libraries.
set(LIB_FOO_LIBRARIES
"$<$<NOT:$<CONFIG:DEBUG>>:${LIB_FOO_RELEASE}>"
"$<$<CONFIG:DEBUG>:${LIB_FOO_DEBUG}>"
)
这篇关于使用Visual Studio调试和发布的find_package的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!