From 75732c512a0766a95dc01213c3ff5c66c2141a72 Mon Sep 17 00:00:00 2001 From: BGQ99 <1132767344@qq.com> Date: Sat, 22 Aug 2026 03:04:48 +0000 Subject: [PATCH] fix(cmake): prioritize discovered zlib headers for brpc sources Explicitly discover zlib and use the ZLIB::ZLIB target instead of the bare z library name. Place the discovered zlib include directories before include paths inherited from parent projects when compiling SOURCES_LIB. This prevents protobuf's gzip_stream.h from resolving an unrelated zlib.h, such as the header provided by Crypto++. This keeps standalone builds working while fixing add_subdirectory integration with dependency prefixes such as vcpkg. --- CMakeLists.txt | 3 ++- src/CMakeLists.txt | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9419ac3a76..93113b6fc4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -227,6 +227,7 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") endif() find_package(Protobuf REQUIRED) +find_package(ZLIB REQUIRED) if(Protobuf_VERSION VERSION_GREATER 4.21) # required by absl set(BRPC_CXX_STANDARD 17) @@ -351,7 +352,7 @@ set(DYNAMIC_LIB ${CMAKE_THREAD_LIBS_INIT} ${THRIFT_LIB} dl - z) + ZLIB::ZLIB) if(WITH_BORINGSSL) list(APPEND DYNAMIC_LIB ${BORINGSSL_SSL_LIBRARY}) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 17fe2154bf..2004df52ab 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -21,6 +21,10 @@ add_dependencies(SOURCES_LIB PROTO_LIB) target_link_libraries(BUTIL_LIB PRIVATE brpc_common_config) target_link_libraries(SOURCES_LIB PRIVATE brpc_common_config) +# protobuf/io/gzip_stream.h includes . Prioritize the discovered +# zlib headers over include directories inherited from parent projects. +target_include_directories(SOURCES_LIB BEFORE PRIVATE ${ZLIB_INCLUDE_DIRS}) + # shared library needs POSITION_INDEPENDENT_CODE set_property(TARGET ${SOURCES_LIB} PROPERTY POSITION_INDEPENDENT_CODE 1) set_property(TARGET ${BUTIL_LIB} PROPERTY POSITION_INDEPENDENT_CODE 1)