简体   繁体   中英

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. I'm able to link my Debug executable with Debug built library and similar with Release and Release using:

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:

cmake ../
cmake --build .

Then zlib and my executable is built in Debug version and my executable is linked with that zlib Debug version.

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? How to achieve that using 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)

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...).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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