Skip to content

Commit 0c0d192

Browse files
build: Add extensive docs on visibility issues
1 parent 4d25990 commit 0c0d192

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

include/secp256k1.h

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,35 +133,56 @@ typedef int (*secp256k1_nonce_function)(
133133
# define SECP256K1_NO_BUILD
134134
#endif
135135

136-
/* Symbol visibility. See https://gcc.gnu.org/wiki/Visibility */
137-
/* DLL_EXPORT is defined internally for shared builds */
136+
/* Symbol visibility. */
138137
#if defined(_WIN32) || defined(__CYGWIN__)
139-
# ifdef SECP256K1_BUILD
140-
# ifdef DLL_EXPORT
138+
/* GCC for Windows (e.g., MinGW) and for Cygwin accept the __declspec syntax
139+
* for MVSC compatibility, see "Microsoft Windows Function Attributes" in the
140+
* GCC manual. Note that __attribute__ ((visibility("default"))) is not exactly
141+
* equivalent to __declspec (dllexport), and as a result, we want the latter
142+
* even on GCC, see https://gcc.gnu.org/wiki/Visibility. */
143+
# if defined(SECP256K1_BUILD)
144+
# if defined(DLL_EXPORT)
145+
/* Building libsecp256k1 as a DLL. */
141146
# define SECP256K1_API __declspec (dllexport)
142147
# define SECP256K1_API_VAR extern __declspec (dllexport)
143148
# endif
144149
# elif defined(SECP256K1_IMPORT_AS_STATICLIB) && defined(SECP256K1_IMPORT_AS_DLL)
145150
# error "At most one of SECP256K1_IMPORT_AS_STATICLIB and SECP256K1_IMPORT_AS_DLL must be defined."
146151
# elif defined(SECP256K1_IMPORT_AS_STATICLIB)
152+
/* Linking against static libsecp256k1 requested explicitly. */
147153
# define SECP256K1_API
148154
# define SECP256K1_API_VAR extern
149155
# elif defined(SECP256K1_IMPORT_AS_DLL)
156+
/* Linking against libsecp256k1 DLL requested explicitly. */
150157
# define SECP256K1_API __declspec (dllimport)
151158
# define SECP256K1_API_VAR extern __declspec (dllimport)
152-
# elif defined _MSC_VER
159+
# elif defined(_MSC_VER)
160+
/* No method requested explicitly. The following works on MSVC for both
161+
* static and dynamic linking, as long as if at least one function is
162+
* imported (i.e., not only variables are imported), which should be the case
163+
* for any meaningful program that uses the libsecp256k1 API. The drawback of
164+
* the following is that it may provoke linker warnings LNK4217 and LNK4286.
165+
* See "Windows DLLs" in the libtool manual. */
153166
# define SECP256K1_API
154167
# define SECP256K1_API_VAR extern __declspec (dllimport)
155-
# elif defined DLL_EXPORT
168+
# elif defined(DLL_EXPORT)
169+
/* No method requested explicitly and we're not on MSVC. We make an educated
170+
* guess based on DLL_EXPORT: If the importing program is itself a DLL, then
171+
* it is likely that it also wants to import libsecp256k1 as a DLL. If our
172+
* guess is wrong, the user will need to make a specific request using the
173+
* macros checked above. See "Windows DLLs" in the libtool manual. */
156174
# define SECP256K1_API __declspec (dllimport)
157175
# define SECP256K1_API_VAR extern __declspec (dllimport)
158176
# endif
159177
#endif
160178
#ifndef SECP256K1_API
161179
# if defined(__GNUC__) && (__GNUC__ >= 4) && defined(SECP256K1_BUILD)
180+
/* Building libsecp256k1 on non-Windows using GCC or compatible. */
162181
# define SECP256K1_API __attribute__ ((visibility ("default")))
163182
# define SECP256K1_API_VAR extern __attribute__ ((visibility ("default")))
164183
# else
184+
/* Linking against libsecp256k1,
185+
or building libsecp256k1 using an unknown compiler. */
165186
# define SECP256K1_API
166187
# define SECP256K1_API_VAR extern
167188
# endif

0 commit comments

Comments
 (0)