0
votes

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)

1
If this is with Visual Studio you should not mix debug and release. Debug has its own heap and is incompatible with release. There are also differences in the implementation of the standard library between debug and release.drescherjm
cmake --build . --config Release maybe a start if you are not using Visual Studio.drescherjm

1 Answers

1
votes

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.