This is an automated email from the ASF dual-hosted git repository.

mgrigorov pushed a commit to branch branch-1.12
in repository https://gitbox.apache.org/repos/asf/avro.git


The following commit(s) were added to refs/heads/branch-1.12 by this push:
     new db5502351a AVRO-4221: [C++] Allow using symbol visibility annotations 
also on non-Windows platforms (#3602)
db5502351a is described below

commit db5502351a76d311f9f51ea761298a25fc2637a0
Author: Stephan Lachnit <[email protected]>
AuthorDate: Thu Jan 22 13:27:36 2026 +0100

    AVRO-4221: [C++] Allow using symbol visibility annotations also on 
non-Windows platforms (#3602)
    
    In GCC and clang the symbol visibility behavior of MSVC can be mirror using 
`-fvisibility=hidden`.
    This allows to more easily test that symbol visbility annotations work 
correctly and can potentially lead to smaller binaries.
    The default behavior on non-Windows platforms is not changed with this 
commit.
    
    See also https://gcc.gnu.org/wiki/Visibility
    
    Also fixes a tiny mistake where the _WIN32 macro was used to silence a MSVC 
warning where the _MSC_VER macro should have been used instead.
---
 lang/c++/impl/json/JsonDom.hh                   |  4 +--
 lang/c++/include/avro/Compiler.hh               |  4 +--
 lang/c++/include/avro/Config.hh                 | 40 ++++++++++++++++++-------
 lang/c++/include/avro/buffer/BufferStream.hh    |  4 +--
 lang/c++/include/avro/buffer/BufferStreambuf.hh |  4 +--
 5 files changed, 38 insertions(+), 18 deletions(-)

diff --git a/lang/c++/impl/json/JsonDom.hh b/lang/c++/impl/json/JsonDom.hh
index 75728c6bad..b2be02b30c 100644
--- a/lang/c++/impl/json/JsonDom.hh
+++ b/lang/c++/impl/json/JsonDom.hh
@@ -31,7 +31,7 @@
 
 namespace avro {
 
-class AVRO_DECL InputStream;
+class InputStream;
 
 namespace json {
 class Entity;
@@ -59,7 +59,7 @@ enum class EntityType {
     Obj
 };
 
-const char *typeToString(EntityType t);
+AVRO_DECL const char *typeToString(EntityType t);
 
 inline std::ostream &operator<<(std::ostream &os, EntityType et) {
     return os << typeToString(et);
diff --git a/lang/c++/include/avro/Compiler.hh 
b/lang/c++/include/avro/Compiler.hh
index bdcbb355e2..7a97c4f4d5 100644
--- a/lang/c++/include/avro/Compiler.hh
+++ b/lang/c++/include/avro/Compiler.hh
@@ -25,14 +25,14 @@
 
 namespace avro {
 
-class AVRO_DECL InputStream;
+class InputStream;
 
 /// This class is used to implement an avro spec parser using a flex/bison
 /// compiler.  In order for the lexer to be reentrant, this class provides a
 /// lexer object for each parse.  The bison parser also uses this class to
 /// build up an avro parse tree as the avro spec is parsed.
 
-class AVRO_DECL ValidSchema;
+class ValidSchema;
 
 /// Given a stream containing a JSON schema, compiles the schema to a
 /// ValidSchema object.  Throws if the schema cannot be compiled to a valid
diff --git a/lang/c++/include/avro/Config.hh b/lang/c++/include/avro/Config.hh
index dd2438debb..5f5becc0dc 100644
--- a/lang/c++/include/avro/Config.hh
+++ b/lang/c++/include/avro/Config.hh
@@ -19,25 +19,45 @@
 #ifndef avro_Config_hh
 #define avro_Config_hh
 
-// Windows DLL support
-
-#ifdef _WIN32
+#ifdef _MSC_VER
 #pragma warning(disable : 4275 4251)
+#endif // _MSC_VER
+
+/*
+ * Symbol visibility macros:
+ *  - AVRO_DLL_EXPORT annotation for exporting symbols
+ *  - AVRO_DLL_IMPORT annotation for importing symbols
+ *  - AVRO_DLL_HIDDEN annotation for hiding symbols
+ *  - AVRO_DYN_LINK needs to be defined when compiling / linking avro as 
dynamic library
+ *  - AVRO_SOURCE needs to be defined when compiling avro as library
+ *  - AVRO_DECL contains the correct symbol visibility annotation depending on 
AVRO_DYN_LINK and AVRO_SOURCE
+ */
+
+#if defined _WIN32 || defined __CYGWIN__
+#define AVRO_DLL_EXPORT __declspec(dllexport)
+#define AVRO_DLL_IMPORT __declspec(dllimport)
+#define AVRO_DLL_HIDDEN
+#else
+#define AVRO_DLL_EXPORT [[gnu::visibility("default")]]
+#define AVRO_DLL_IMPORT [[gnu::visibility("default")]]
+#define AVRO_DLL_HIDDEN [[gnu::visibility("hidden")]]
+#endif // _WIN32 || __CYGWIN__
 
-#if defined(AVRO_DYN_LINK)
+#ifdef AVRO_DYN_LINK
 #ifdef AVRO_SOURCE
-#define AVRO_DECL __declspec(dllexport)
+#define AVRO_DECL AVRO_DLL_EXPORT
 #else
-#define AVRO_DECL __declspec(dllimport)
+#define AVRO_DECL AVRO_DLL_IMPORT
 #endif // AVRO_SOURCE
 #endif // AVRO_DYN_LINK
 
-#include <intsafe.h>
-using ssize_t = SSIZE_T;
-#endif // _WIN32
-
 #ifndef AVRO_DECL
 #define AVRO_DECL
 #endif
 
+#ifdef _WIN32
+#include <intsafe.h>
+using ssize_t = SSIZE_T;
+#endif // _WIN32
+
 #endif
diff --git a/lang/c++/include/avro/buffer/BufferStream.hh 
b/lang/c++/include/avro/buffer/BufferStream.hh
index a8510adaa1..3aeda37f0f 100644
--- a/lang/c++/include/avro/buffer/BufferStream.hh
+++ b/lang/c++/include/avro/buffer/BufferStream.hh
@@ -35,7 +35,7 @@ namespace avro {
  *
  **/
 
-class AVRO_DECL ostream : public std::ostream {
+class ostream : public std::ostream {
 
 public:
     /// Default constructor, creates a new OutputBuffer.
@@ -65,7 +65,7 @@ protected:
  *
  **/
 
-class AVRO_DECL istream : public std::istream {
+class istream : public std::istream {
 
 public:
     /// Constructor, requires an InputBuffer to read from.
diff --git a/lang/c++/include/avro/buffer/BufferStreambuf.hh 
b/lang/c++/include/avro/buffer/BufferStreambuf.hh
index 42eb20c21c..38cc91fce6 100644
--- a/lang/c++/include/avro/buffer/BufferStreambuf.hh
+++ b/lang/c++/include/avro/buffer/BufferStreambuf.hh
@@ -41,7 +41,7 @@ namespace avro {
  * but we have no need since all writes are immediately stored in the buffer.
  **/
 
-class AVRO_DECL ostreambuf : public std::streambuf {
+class ostreambuf : public std::streambuf {
 
 public:
     /// Default constructor creates a new OutputBuffer.
@@ -86,7 +86,7 @@ private:
  *
  **/
 
-class AVRO_DECL istreambuf : public std::streambuf {
+class istreambuf : public std::streambuf {
 
 public:
     /// Default constructor requires an InputBuffer to read from.

Reply via email to