使用ExternalProject_Add为CMake项目添加zlib依赖时遭遇缓存加载失败问题求助
Hey there, that "could not load cache" error tells us CMake can't find the CMakeCache.txt file in your zlib build directory (${DEPS_BUILD}/zlib) — which means the configuration step for zlib never completed properly. Let's break down the fixes based on your setup:
1. Explicitly Add Configuration & Build Commands
Your current ExternalProject_Add setup doesn't specify a clear configuration step for the local zlib source (your git submodule). ExternalProject needs a nudge to run CMake and generate the necessary build cache. Update your zlib config like this:
ExternalProject_Add(zlib PREFIX ${CMAKE_CURRENT_BINARY_DIR}/deps/zlib INSTALL_DIR ${CMAKE_INSTALL_PREFIX} DOWNLOAD_DIR ${DEPS_SRC}/zlib TMP_DIR ${DEPS_TMP}/zlib STAMP_DIR ${DEPS_STAMP}/zlib LOG_DIR ${DEPS_LOG}/zlib BINARY_DIR ${DEPS_BUILD}/zlib SOURCE_DIR ${DEPS}/zlib # Double-check this points directly to your zlib submodule root # Force CMake to configure the zlib source and generate the build cache CONFIGURE_COMMAND ${CMAKE_COMMAND} -S ${DEPS}/zlib -B ${DEPS_BUILD}/zlib -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX} -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} -A x64 # Critical for VS 2017 64-bit builds # Explicit build command to avoid ambiguity BUILD_COMMAND ${CMAKE_COMMAND} --build ${DEPS_BUILD}/zlib --config ${CMAKE_BUILD_TYPE} INSTALL_COMMAND cmake -E echo "Skipping install step for dependency zlib" )
The key changes here:
- We're explicitly running CMake's configure step with
CONFIGURE_COMMANDto generate the missingCMakeCache.txt - We're passing all necessary environment variables (build type, compilers, target architecture) to match your main project's setup — this ensures zlib builds correctly for VS 2017 64-bit
2. Verify Your SOURCE_DIR Path
Make sure ${DEPS}/zlib points directly to the root of your zlib git submodule, and that the directory contains a valid CMakeLists.txt file. Zlib has provided CMake configs since version 1.2.11; if your submodule uses an older version, you'll need to update it or add a custom CMake config for zlib.
3. Clean Up & Rebuild
Sometimes stale directories or permissions can cause issues. Delete the entire deps folder in your build directory, then regenerate your VS solution and try building zlib again. This ensures CMake creates all dependency directories from scratch without leftover junk.
Why Did TurboJPEG Work?
Chances are the TurboJPEG submodule has a more flexible CMake setup that played nicely with ExternalProject's default behavior. Zlib requires a bit more explicit configuration, especially when targeting a specific platform like VS 2017 64-bit.
内容的提问来源于stack exchange,提问作者rbaleksandar

