From 52938d00107626aaedbafb2ea9e9fb50f3b95138 Mon Sep 17 00:00:00 2001 From: Stephan Lachnit Date: Wed, 17 Dec 2025 15:24:11 +0100 Subject: [PATCH] [C++] Allow using symbol visibility annotations also on non-Windows platforms 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 | 8 ++++---- lang/c++/include/avro/Config.hh | 20 ++++++++++++++++---- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/lang/c++/impl/json/JsonDom.hh b/lang/c++/impl/json/JsonDom.hh index 75728c6badd..b2be02b30cd 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 911a8aed0cd..9c9ec8a71fb 100644 --- a/lang/c++/include/avro/Compiler.hh +++ b/lang/c++/include/avro/Compiler.hh @@ -26,15 +26,15 @@ 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 Name; -class AVRO_DECL ValidSchema; +class Name; +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 @@ -60,7 +60,7 @@ AVRO_DECL ValidSchema compileJsonSchemaFromString(const std::string &input); AVRO_DECL ValidSchema compileJsonSchemaFromFile(const char *filename); -AVRO_DECL ValidSchema compileJsonSchemaWithNamedReferences(std::istream &is, +AVRO_DECL ValidSchema compileJsonSchemaWithNamedReferences(std::istream &is, const std::map &namedReferences); } // namespace avro diff --git a/lang/c++/include/avro/Config.hh b/lang/c++/include/avro/Config.hh index dd2438debbe..0f7ea37380b 100644 --- a/lang/c++/include/avro/Config.hh +++ b/lang/c++/include/avro/Config.hh @@ -21,17 +21,29 @@ // Windows DLL support -#ifdef _WIN32 +#ifdef _MSC_VER #pragma warning(disable : 4275 4251) +#endif // _MSC_VER + +#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 +#ifdef _WIN32 #include using ssize_t = SSIZE_T; #endif // _WIN32