From ff6133ab49b364a883a55ba75c39e520fea6245b Mon Sep 17 00:00:00 2001 From: Corentin Le Molgat Date: Fri, 9 Jan 2026 07:33:40 -0800 Subject: [PATCH] cmake: Fix declspec of gtest flag when using BUILD_SHARED_LIBS=ON and absl On windows flags declaration must be prepend by `GTEST_API_` to have the correct declspec (dllexport or dllimport) This patch also fix super build integration of googletest by adding the necessary `INSTALL_RPATH` and `$) + $ + $ + ) + if(APPLE) + set_target_properties(${name} PROPERTIES + INSTALL_RPATH "@loader_path") + elseif(UNIX) + set_target_properties(${name} PROPERTIES + INSTALL_RPATH "$ORIGIN") + endif() endif() if (DEFINED GTEST_HAS_PTHREAD) target_link_libraries(${name} PUBLIC Threads::Threads) @@ -226,9 +234,8 @@ function(cxx_executable_with_flags name cxx_flags libs) COMPILE_FLAGS "${cxx_flags}") endif() if (BUILD_SHARED_LIBS) - set_target_properties(${name} - PROPERTIES - COMPILE_DEFINITIONS "GTEST_LINKED_AS_SHARED_LIBRARY=1") + target_compile_definitions(${name} PRIVATE + "GTEST_LINKED_AS_SHARED_LIBRARY=1") endif() # To support mixing linking in static and dynamic libraries, link each # library in with an extra call to target_link_libraries. diff --git a/googletest/include/gtest/internal/gtest-port.h b/googletest/include/gtest/internal/gtest-port.h index 3aa92057..69eaad10 100644 --- a/googletest/include/gtest/internal/gtest-port.h +++ b/googletest/include/gtest/internal/gtest-port.h @@ -832,10 +832,10 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION; #ifndef GTEST_API_ #ifdef _MSC_VER -#if defined(GTEST_LINKED_AS_SHARED_LIBRARY) && GTEST_LINKED_AS_SHARED_LIBRARY -#define GTEST_API_ __declspec(dllimport) -#elif defined(GTEST_CREATE_SHARED_LIBRARY) && GTEST_CREATE_SHARED_LIBRARY +#if defined(GTEST_CREATE_SHARED_LIBRARY) && GTEST_CREATE_SHARED_LIBRARY #define GTEST_API_ __declspec(dllexport) +#elif defined(GTEST_LINKED_AS_SHARED_LIBRARY) && GTEST_LINKED_AS_SHARED_LIBRARY +#define GTEST_API_ __declspec(dllimport) #endif #elif GTEST_INTERNAL_HAVE_CPP_ATTRIBUTE(gnu::visibility) #define GTEST_API_ [[gnu::visibility("default")]] @@ -2252,11 +2252,11 @@ using TimeInMillis = int64_t; // Represents time in milliseconds. // Macros for declaring flags. #define GTEST_DECLARE_bool_(name) \ - ABSL_DECLARE_FLAG(bool, GTEST_FLAG_NAME_(name)) + GTEST_API_ ABSL_DECLARE_FLAG(bool, GTEST_FLAG_NAME_(name)) #define GTEST_DECLARE_int32_(name) \ - ABSL_DECLARE_FLAG(int32_t, GTEST_FLAG_NAME_(name)) + GTEST_API_ ABSL_DECLARE_FLAG(int32_t, GTEST_FLAG_NAME_(name)) #define GTEST_DECLARE_string_(name) \ - ABSL_DECLARE_FLAG(std::string, GTEST_FLAG_NAME_(name)) + GTEST_API_ ABSL_DECLARE_FLAG(std::string, GTEST_FLAG_NAME_(name)) #define GTEST_FLAG_SAVER_ ::absl::FlagSaver