Skip to content

Commit ae9db95

Browse files
committed
build: Introduce SECP256K1_STATIC macro for Windows users
It is a non-Libtool-specific way to explicitly specify the user's intention to consume a static `libseck256k1`. This change allows to get rid of MSVC linker warnings LNK4217 and LNK4286. Also, it makes possible to merge the `SECP256K1_API` and `SECP256K1_API_VAR` into one.
1 parent 7966aee commit ae9db95

File tree

6 files changed

+11
-16
lines changed

6 files changed

+11
-16
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
- Document `doc/ellswift.md` which explains the mathematical background of the scheme.
1515
- The [paper](https://eprint.iacr.org/2022/759) on which the scheme is based.
1616

17+
#### Changed
18+
- When consuming libsecp256k1 as a static library on Windows, the user must now define the `SECP256K1_STATIC` macro before including `secp256k1.h`.
19+
1720
## [0.3.2] - 2023-05-13
1821
We strongly recommend updating to 0.3.2 if you use or plan to use GCC >=13 to compile libsecp256k1. When in doubt, check the GCC version using `gcc -v`.
1922

Makefile.am

+3-3
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ endif
153153
if USE_EXAMPLES
154154
noinst_PROGRAMS += ecdsa_example
155155
ecdsa_example_SOURCES = examples/ecdsa.c
156-
ecdsa_example_CPPFLAGS = -I$(top_srcdir)/include
156+
ecdsa_example_CPPFLAGS = -I$(top_srcdir)/include -DSECP256K1_STATIC
157157
ecdsa_example_LDADD = libsecp256k1.la
158158
ecdsa_example_LDFLAGS = -static
159159
if BUILD_WINDOWS
@@ -163,7 +163,7 @@ TESTS += ecdsa_example
163163
if ENABLE_MODULE_ECDH
164164
noinst_PROGRAMS += ecdh_example
165165
ecdh_example_SOURCES = examples/ecdh.c
166-
ecdh_example_CPPFLAGS = -I$(top_srcdir)/include
166+
ecdh_example_CPPFLAGS = -I$(top_srcdir)/include -DSECP256K1_STATIC
167167
ecdh_example_LDADD = libsecp256k1.la
168168
ecdh_example_LDFLAGS = -static
169169
if BUILD_WINDOWS
@@ -174,7 +174,7 @@ endif
174174
if ENABLE_MODULE_SCHNORRSIG
175175
noinst_PROGRAMS += schnorr_example
176176
schnorr_example_SOURCES = examples/schnorr.c
177-
schnorr_example_CPPFLAGS = -I$(top_srcdir)/include
177+
schnorr_example_CPPFLAGS = -I$(top_srcdir)/include -DSECP256K1_STATIC
178178
schnorr_example_LDADD = libsecp256k1.la
179179
schnorr_example_LDFLAGS = -static
180180
if BUILD_WINDOWS

configure.ac

-6
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,6 @@ AC_DEFUN([SECP_TRY_APPEND_DEFAULT_CFLAGS], [
127127
SECP_TRY_APPEND_CFLAGS([-wd4267], $1) # Disable warning C4267 "'var' : conversion from 'size_t' to 'type', possible loss of data".
128128
# Eliminate deprecation warnings for the older, less secure functions.
129129
CPPFLAGS="-D_CRT_SECURE_NO_WARNINGS $CPPFLAGS"
130-
# We pass -ignore:4217 to the MSVC linker to suppress warning 4217 when
131-
# importing variables from a statically linked secp256k1.
132-
# (See the libtool manual, section "Windows DLLs" for background.)
133-
# Unfortunately, libtool tries to be too clever and strips "-Xlinker arg"
134-
# into "arg", so this will be " -Xlinker -ignore:4217" after stripping.
135-
LDFLAGS="-Xlinker -Xlinker -Xlinker -ignore:4217 $LDFLAGS"
136130
fi
137131
])
138132
SECP_TRY_APPEND_DEFAULT_CFLAGS(SECP_CFLAGS)

examples/CMakeLists.txt

-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ target_link_libraries(example INTERFACE
66
secp256k1
77
$<$<PLATFORM_ID:Windows>:bcrypt>
88
)
9-
if(NOT BUILD_SHARED_LIBS AND MSVC)
10-
target_link_options(example INTERFACE /IGNORE:4217)
11-
endif()
129

1310
add_executable(ecdsa_example ecdsa.c)
1411
target_link_libraries(ecdsa_example example)

include/secp256k1.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,10 @@ typedef int (*secp256k1_nonce_function)(
141141
# define SECP256K1_API __declspec (dllexport)
142142
# define SECP256K1_API_VAR extern __declspec (dllexport)
143143
# endif
144-
# elif defined _MSC_VER
145-
# define SECP256K1_API
146-
# define SECP256K1_API_VAR extern __declspec (dllimport)
147-
# elif defined DLL_EXPORT
144+
/* The user must define SECP256K1_STATIC when consuming libsecp256k1 as a static
145+
* library on Windows. */
146+
# elif !defined(SECP256K1_STATIC)
147+
/* Consuming libsecp256k1 as a DLL. */
148148
# define SECP256K1_API __declspec (dllimport)
149149
# define SECP256K1_API_VAR extern __declspec (dllimport)
150150
# endif

src/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ endif()
2424
# This matches libtool's usage of DLL_EXPORT
2525
if(WIN32)
2626
set_target_properties(secp256k1 PROPERTIES DEFINE_SYMBOL "DLL_EXPORT")
27+
target_compile_definitions(secp256k1 INTERFACE $<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:SECP256K1_STATIC>)
2728
endif()
2829

2930
# Object libs don't know if they're being built for a shared or static lib.

0 commit comments

Comments
 (0)