From 5b9b747fd2c3d86d16effa20b03848998b9f1854 Mon Sep 17 00:00:00 2001 From: Felix-Gong Date: Sun, 14 Jun 2026 14:27:50 +0000 Subject: [PATCH] use -fno-access-control instead of -Dprivate=public for tests Replace the -Dprivate=public -Dprotected=public macro hack with the -fno-access-control compiler flag in both CMake and Makefile builds. The macro approach caused redeclaration errors with standard library headers (, , etc.) that use private access specifiers internally. -fno-access-control is a GCC/Clang compiler flag that cleanly disables access control checking without affecting header parsing, eliminating the need for the sstream_workaround.h workaround logic. The Bazel build already uses -fno-access-control at test/BUILD.bazel, so this makes CMake and Makefile builds consistent with Bazel. Signed-off-by: Felix-Gong --- test/CMakeLists.txt | 4 ++-- test/Makefile | 4 ++-- test/sstream_workaround.h | 18 +++++++----------- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index ade7350f5a..df8fedce49 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -53,8 +53,8 @@ else() endif() set(CMAKE_CPP_FLAGS "${CMAKE_CPP_FLAGS} ${DEFINE_CLOCK_GETTIME} -DBRPC_WITH_GLOG=${WITH_GLOG_VAL} -DBRPC_WITH_RDMA=${WITH_RDMA_VAL}") -set(CMAKE_CPP_FLAGS "${CMAKE_CPP_FLAGS} -DBTHREAD_USE_FAST_PTHREAD_MUTEX -D__const__=__unused__ -D_GNU_SOURCE -DUSE_SYMBOLIZE -DNO_TCMALLOC -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS -DUNIT_TEST -Dprivate=public -Dprotected=public -DBVAR_NOT_LINK_DEFAULT_VARIABLES -D__STRICT_ANSI__ -include ${PROJECT_SOURCE_DIR}/test/sstream_workaround.h") -set(CMAKE_CXX_FLAGS "${CMAKE_CPP_FLAGS} -g -O2 -pipe -Wall -W -fPIC -fstrict-aliasing -Wno-invalid-offsetof -Wno-unused-parameter -fno-omit-frame-pointer") +set(CMAKE_CPP_FLAGS "${CMAKE_CPP_FLAGS} -DBTHREAD_USE_FAST_PTHREAD_MUTEX -D__const__=__unused__ -D_GNU_SOURCE -DUSE_SYMBOLIZE -DNO_TCMALLOC -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS -DUNIT_TEST -DBVAR_NOT_LINK_DEFAULT_VARIABLES -D__STRICT_ANSI__") +set(CMAKE_CXX_FLAGS "${CMAKE_CPP_FLAGS} -g -O2 -pipe -Wall -W -fPIC -fstrict-aliasing -Wno-invalid-offsetof -Wno-unused-parameter -fno-omit-frame-pointer -fno-access-control") use_cxx11() if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") diff --git a/test/Makefile b/test/Makefile index 30e196bbce..15169937ef 100644 --- a/test/Makefile +++ b/test/Makefile @@ -18,8 +18,8 @@ NEED_GPERFTOOLS=1 NEED_GTEST=1 include ../config.mk -CPPFLAGS+=-DBTHREAD_USE_FAST_PTHREAD_MUTEX -D_GNU_SOURCE -DUSE_SYMBOLIZE -DNO_TCMALLOC -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS -DUNIT_TEST -Dprivate=public -Dprotected=public -DBVAR_NOT_LINK_DEFAULT_VARIABLES --include sstream_workaround.h -CXXFLAGS+=$(CPPFLAGS) -pipe -Wall -W -fPIC -fstrict-aliasing -Wno-invalid-offsetof -Wno-unused-parameter -fno-omit-frame-pointer -std=c++0x +CPPFLAGS+=-DBTHREAD_USE_FAST_PTHREAD_MUTEX -D_GNU_SOURCE -DUSE_SYMBOLIZE -DNO_TCMALLOC -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS -DUNIT_TEST -DBVAR_NOT_LINK_DEFAULT_VARIABLES +CXXFLAGS+=$(CPPFLAGS) -pipe -Wall -W -fPIC -fstrict-aliasing -Wno-invalid-offsetof -Wno-unused-parameter -fno-omit-frame-pointer -fno-access-control -std=c++0x # On Darwin, only gtest library is needed, other libraries have been linked in `brpc.dbg.dylib` ifeq ($(SYSTEM),Darwin) diff --git a/test/sstream_workaround.h b/test/sstream_workaround.h index 05934e5597..06cf39ac15 100644 --- a/test/sstream_workaround.h +++ b/test/sstream_workaround.h @@ -18,16 +18,12 @@ #ifndef BUTIL_TEST_SSTREAM_WORKAROUND #define BUTIL_TEST_SSTREAM_WORKAROUND -// defining private as public makes it fail to compile sstream with gcc5.x like this: -// "error: ‘struct std::__cxx11::basic_stringbuf<_CharT, _Traits, _Alloc>:: -// __xfer_bufptrs’ redeclared with different access" - -#ifdef private -# undef private -# include -# define private public -#else -# include -#endif +// Previously, tests used -Dprivate=public to access private members. +// This caused redeclaration errors with standard library headers (, +// , etc.) that use private access specifiers internally. +// +// Now tests use -fno-access-control (compiler flag) instead, which cleanly +// disables access control without affecting header parsing. This header is +// kept for backward compatibility but no longer needs the workaround logic. #endif // BUTIL_TEST_SSTREAM_WORKAROUND