Skip to content

Commit 6385722

Browse files
[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.
1 parent 026d678 commit 6385722

File tree

5 files changed

+38
-18
lines changed

5 files changed

+38
-18
lines changed

lang/c++/impl/json/JsonDom.hh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
namespace avro {
3333

34-
class AVRO_DECL InputStream;
34+
class InputStream;
3535

3636
namespace json {
3737
class Entity;
@@ -59,7 +59,7 @@ enum class EntityType {
5959
Obj
6060
};
6161

62-
const char *typeToString(EntityType t);
62+
AVRO_DECL const char *typeToString(EntityType t);
6363

6464
inline std::ostream &operator<<(std::ostream &os, EntityType et) {
6565
return os << typeToString(et);

lang/c++/include/avro/Compiler.hh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525

2626
namespace avro {
2727

28-
class AVRO_DECL InputStream;
28+
class InputStream;
2929

3030
/// This class is used to implement an avro spec parser using a flex/bison
3131
/// compiler. In order for the lexer to be reentrant, this class provides a
3232
/// lexer object for each parse. The bison parser also uses this class to
3333
/// build up an avro parse tree as the avro spec is parsed.
3434

35-
class AVRO_DECL ValidSchema;
35+
class ValidSchema;
3636

3737
/// Given a stream containing a JSON schema, compiles the schema to a
3838
/// ValidSchema object. Throws if the schema cannot be compiled to a valid

lang/c++/include/avro/Config.hh

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,45 @@
1919
#ifndef avro_Config_hh
2020
#define avro_Config_hh
2121

22-
// Windows DLL support
23-
24-
#ifdef _WIN32
22+
#ifdef _MSC_VER
2523
#pragma warning(disable : 4275 4251)
24+
#endif // _MSC_VER
25+
26+
/*
27+
* Symbol visibility macros:
28+
* - AVRO_DLL_EXPORT annotation for exporting symbols
29+
* - AVRO_DLL_IMPORT annotation for importing symbols
30+
* - AVRO_DLL_HIDDEN annotation for hiding symbols
31+
* - AVRO_DYN_LINK needs to be defined when compiling / linking avro as dynamic library
32+
* - AVRO_SOURCE needs to be defined when compiling avro as library
33+
* - AVRO_DECL contains the correct symbol visibility annotation depending on the two macros above
34+
*/
35+
36+
#if defined _WIN32 || defined __CYGWIN__
37+
#define AVRO_DLL_EXPORT __declspec(dllexport)
38+
#define AVRO_DLL_IMPORT __declspec(dllimport)
39+
#define AVRO_DLL_HIDDEN
40+
#else
41+
#define AVRO_DLL_EXPORT [[gnu::visibility("default")]]
42+
#define AVRO_DLL_IMPORT [[gnu::visibility("default")]]
43+
#define AVRO_DLL_HIDDEN [[gnu::visibility("hidden")]]
44+
#endif // _WIN32 || __CYGWIN__
2645

27-
#if defined(AVRO_DYN_LINK)
46+
#ifdef AVRO_DYN_LINK
2847
#ifdef AVRO_SOURCE
29-
#define AVRO_DECL __declspec(dllexport)
48+
#define AVRO_DECL AVRO_DLL_EXPORT
3049
#else
31-
#define AVRO_DECL __declspec(dllimport)
50+
#define AVRO_DECL AVRO_DLL_IMPORT
3251
#endif // AVRO_SOURCE
3352
#endif // AVRO_DYN_LINK
3453

35-
#include <intsafe.h>
36-
using ssize_t = SSIZE_T;
37-
#endif // _WIN32
38-
3954
#ifndef AVRO_DECL
4055
#define AVRO_DECL
4156
#endif
4257

58+
#ifdef _WIN32
59+
#include <intsafe.h>
60+
using ssize_t = SSIZE_T;
61+
#endif // _WIN32
62+
4363
#endif

lang/c++/include/avro/buffer/BufferStream.hh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace avro {
3535
*
3636
**/
3737

38-
class AVRO_DECL ostream : public std::ostream {
38+
class ostream : public std::ostream {
3939

4040
public:
4141
/// Default constructor, creates a new OutputBuffer.
@@ -65,7 +65,7 @@ protected:
6565
*
6666
**/
6767

68-
class AVRO_DECL istream : public std::istream {
68+
class istream : public std::istream {
6969

7070
public:
7171
/// Constructor, requires an InputBuffer to read from.

lang/c++/include/avro/buffer/BufferStreambuf.hh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ namespace avro {
4141
* but we have no need since all writes are immediately stored in the buffer.
4242
**/
4343

44-
class AVRO_DECL ostreambuf : public std::streambuf {
44+
class ostreambuf : public std::streambuf {
4545

4646
public:
4747
/// Default constructor creates a new OutputBuffer.
@@ -86,7 +86,7 @@ private:
8686
*
8787
**/
8888

89-
class AVRO_DECL istreambuf : public std::streambuf {
89+
class istreambuf : public std::streambuf {
9090

9191
public:
9292
/// Default constructor requires an InputBuffer to read from.

0 commit comments

Comments
 (0)