简体   繁体   English

如何使用 CMake FetchContent 将调试可执行文件与发布库链接?

[英]How to link debug executable with release library using CMake FetchContent?

I would like to link my Debug executable with external library built in Release version using FetchContent.我想使用 FetchContent 将我的 Debug 可执行文件与 Release 版本中内置的外部库链接起来。 I'm able to link my Debug executable with Debug built library and similar with Release and Release using:我可以使用以下方法将我的 Debug 可执行文件与 Debug 构建库链接起来,并与 Release 和 Release 类似:

project(CMakeDemo)
set(FETCHCONTENT_QUIET OFF)
FetchContent_Declare(
      ZLIB
      URL  https://zlib.net/zlib-1.2.11.tar.gz
    )
FetchContent_MakeAvailable(ZLIB)
    
add_executable(CMakeDemo main.cpp)
target_link_libraries(CMakeDemo ZLIB)

So when I execute from a build directory on Windows:因此,当我从 Windows 上的构建目录执行时:

cmake ../
cmake --build .

Then zlib and my executable is built in Debug version and my executable is linked with that zlib Debug version.然后 zlib 和我的可执行文件在 Debug 版本中构建,我的可执行文件与该 zlib Debug 版本链接。

But how to enhance the CMake to build my executable in Debug version but zlib in Release version and link my Debug executable with the zlib Release version?但是如何增强 CMake 以在调试版本中构建我的可执行文件,但在发布版本中构建 zlib 并将我的调试可执行文件与 zlib 发布版本链接? How to achieve that using FetchContent_Declare ?如何使用 FetchContent_Declare 来实现

(I believe this has to be some common approach because for example when someone wants to use Google Test framework or zlib in a project then for sure he wants to use these external libraries in Release version always) (我相信这必须是一些常见的方法,因为例如当有人想在项目中使用谷歌测试框架或 zlib 时,他肯定想在发布版本中使用这些外部库)

FetchContent() will integrate the dependency, here ZLIB, in your worktree like an add_subdirectory() so flags will be identical (if ZLIB is correctly configured to be use as subproject, spoiler: this is not the case you'll need to patch it...). FetchContent()将在您的工作树中集成依赖项,此处为 ZLIB,就像add_subdirectory()一样,因此标志将是相同的(如果 ZLIB 被正确配置为用作子项目,剧透:这不是您需要修补它的情况...)。

If you really want to build it in Release, you should try to use ExternalProject() and execute_process() to build and install it at configure time then you can use find_package() to retrieve this pre-installed dependency.如果你真的想在 Release 中构建它,你应该尝试使用ExternalProject()execute_process()在配置时构建和安装它,然后你可以使用find_package()来检索这个预安装的依赖项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM