2 * Cross platform defines for exporting symbols from shared libraries
4 * Wireshark - Network traffic analyzer
5 * By Balint Reczey <balint@balintreczey.hu>
6 * Copyright 2013 Balint Reczey
8 * SPDX-License-Identifier: GPL-2.0-or-later
11 #include "ws_compiler_tests.h"
13 /** Reset symbol export behavior.
14 * If you {un}define WS_BUILD_DLL on the fly you'll have to define this
17 #ifdef RESET_SYMBOL_EXPORT
19 #ifdef SYMBOL_EXPORT_H
20 #undef SYMBOL_EXPORT_H
27 #ifdef WS_DLL_PUBLIC_DEF
28 #undef WS_DLL_PUBLIC_DEF
35 #endif /* RESET_SYMBOL_EXPORT */
37 #ifndef SYMBOL_EXPORT_H
38 #define SYMBOL_EXPORT_H
41 * NOTE: G_HAVE_GNUC_VISIBILITY is defined only if all of
43 * __attribute__ ((visibility ("hidden")))
45 * __attribute__ ((visibility ("internal")))
47 * __attribute__ ((visibility ("protected")))
49 * __attribute__ ((visibility ("default")))
51 * are supported, and at least some versions of GCC from Apple support
52 * "default" and "hidden" but not "internal" or "protected", so it
53 * shouldn't be used to determine whether "hidden" or "default" is
56 * This also means that we shouldn't use G_GNUC_INTERNAL instead of
57 * WS_DLL_LOCAL, as GLib uses G_HAVE_GNUC_VISIBILITY to determine
58 * whether to use __attribute__ ((visibility ("hidden"))) for
59 * G_GNUC_INTERNAL, and that will not use it even with compilers
63 /* Originally copied from GCC Wiki at https://gcc.gnu.org/wiki/Visibility */
64 #if defined _WIN32 || defined __CYGWIN__
65 /* Compiling for Windows, so we use the Windows DLL declarations. */
68 * Building a DLL; for all definitions, we want dllexport, and
69 * (presumably so source from DLL and source from a program using the
70 * DLL can both include a header that declares APIs and exported data
71 * for the DLL), for declarations, either dllexport or dllimport will
72 * work (they mean the same thing for a declaration when building a DLL).
76 #define WS_DLL_PUBLIC_DEF __attribute__ ((dllexport))
77 #else /* ! __GNUC__ */
80 * Note: actually gcc seems to also support this syntax.
82 #define WS_DLL_PUBLIC_DEF __declspec(dllexport)
84 #else /* WS_BUILD_DLL */
86 * Building a program; we should only see declarations, not definitions,
87 * with WS_DLL_PUBLIC, and they all represent APIs or data imported
88 * from a DLL, so use dllimport.
90 * For functions, export shouldn't be necessary; for data, it might
91 * be necessary, e.g. if what's declared is an array whose size is
92 * not given in the declaration.
96 #define WS_DLL_PUBLIC_DEF __attribute__ ((dllimport))
97 #elif ! (defined ENABLE_STATIC) /* ! __GNUC__ */
99 * Presumably MSVC, and we're not building all-static.
100 * Note: actually gcc seems to also support this syntax.
102 #define WS_DLL_PUBLIC_DEF __declspec(dllimport)
103 #else /* ! __GNUC__ && ENABLE_STATIC */
105 * Presumably MSVC, and we're building all-static, so we're
106 * not building any DLLs.
108 #define WS_DLL_PUBLIC_DEF
109 #endif /* __GNUC__ */
110 #endif /* WS_BUILD_DLL */
113 * Symbols in a DLL are *not* exported unless they're specifically
114 * flagged as exported, so, for a non-static but non-exported
115 * symbol, we don't have to do anything.
118 #else /* defined _WIN32 || defined __CYGWIN__ */
120 * Compiling for UN*X, where the dllimport and dllexport stuff
121 * is neither necessary nor supported; just specify the
122 * visibility if we have a compiler that supports doing so.
124 #if WS_IS_AT_LEAST_GNUC_VERSION(3,4) \
125 || WS_IS_AT_LEAST_XL_C_VERSION(12,0)
127 * GCC 3.4 or later, or some compiler asserting compatibility with
128 * GCC 3.4 or later, or XL C 13.0 or later, so we have
129 * __attribute__((visibility()).
133 * Symbols exported from libraries.
135 #define WS_DLL_PUBLIC_DEF __attribute__ ((visibility ("default")))
138 * Non-static symbols *not* exported from libraries.
140 #define WS_DLL_LOCAL __attribute__ ((visibility ("hidden")))
141 #elif WS_IS_AT_LEAST_SUNC_VERSION(5,5)
143 * Sun C 5.5 or later, so we have __global and __hidden.
144 * (Sun C 5.9 and later also have __attribute__((visibility()),
145 * but there's no reason to prefer it with Sun C.)
149 * Symbols exported from libraries.
151 #define WS_DLL_PUBLIC_DEF __global
154 * Non-static symbols *not* exported from libraries.
156 #define WS_DLL_LOCAL __hidden
159 * We have neither a way to make stuff not explicitly marked as
160 * visible invisible outside a library nor a way to make stuff
161 * explicitly marked as local invisible outside the library.
165 * Symbols exported from libraries.
167 #define WS_DLL_PUBLIC_DEF
170 * Non-static symbols *not* exported from libraries.
177 * You *must* use this for exported data *declarations*; if you use
178 * WS_DLL_PUBLIC_DEF, some compilers, such as MSVC++, will complain
179 * about array definitions with no size.
181 * You must *not* use this for exported data *definitions*, as that
182 * will, for some compilers, cause warnings about items being initialized
183 * and declared extern.
185 * Either can be used for exported *function* declarations and definitions.
187 #define WS_DLL_PUBLIC WS_DLL_PUBLIC_DEF extern
189 #endif /* SYMBOL_EXPORT_H */
192 * Editor modelines - https://www.wireshark.org/tools/modelines.html
197 * indent-tabs-mode: nil
200 * vi: set shiftwidth=2 tabstop=8 expandtab:
201 * :indentSize=2:tabSize=8:noTabs=true: