This is not a satisfactory solution. We can see that after this commit, user code will always see `WINPTHREAD_API` as empty. I prefer to move it from headers to source files.


--
Best regards,
LIU Hao
From cd7156e5e72c8697e5a50da8f7df65e551e6006d Mon Sep 17 00:00:00 2001
From: LIU Hao <[email protected]>
Date: Sat, 30 Apr 2022 21:17:38 +0800
Subject: [PATCH] winpthreads: Do not use `dllimport` when building 3rd-party
 DLLs

When using libtool to build a 3rd-party DLL that attempts to link
against winpthreads statically, `DLL_EXPORT` is defined when compiling
3rd-party source files, which used to cause winpthreads functions to be
decorated as `dllimport`. As static libraries do not export functions,
this would end up with undefined references.

We only ever need `dllexport` on winpthreads functions when building
winpthreads itself. GNU LD can import them even without the `dllimport`
attribute, so it's unnecessary.

Note: This is not a satisfactory solution. We can see that after this
commit, user code will always see `WINPTHREAD_API` as empty. I prefer to
move it from headers to source files.

Reference: https://sourceforge.net/p/mingw-w64/mailman/message/37647350/
Signed-off-by: LIU Hao <[email protected]>
---
 mingw-w64-libraries/winpthreads/include/pthread.h   | 8 +++-----
 mingw-w64-libraries/winpthreads/include/sched.h     | 8 +++-----
 mingw-w64-libraries/winpthreads/include/semaphore.h | 8 +++-----
 3 files changed, 9 insertions(+), 15 deletions(-)

diff --git a/mingw-w64-libraries/winpthreads/include/pthread.h 
b/mingw-w64-libraries/winpthreads/include/pthread.h
index 5aadb1cfa..d907d69b0 100644
--- a/mingw-w64-libraries/winpthreads/include/pthread.h
+++ b/mingw-w64-libraries/winpthreads/include/pthread.h
@@ -83,13 +83,11 @@ extern "C" {
 /* MSB 8-bit major version, 8-bit minor version, 16-bit patch level.  */
 #define __WINPTHREADS_VERSION 0x00050000
 
-#if defined DLL_EXPORT
-#ifdef IN_WINPTHREAD
+/* Declare exported functions as `dllexport` when building the DLL,
+ * but auto-import them outside winpthread.  */
+#if defined(DLL_EXPORT) && defined(IN_WINPTHREAD)
 #define WINPTHREAD_API __declspec(dllexport)
 #else
-#define WINPTHREAD_API __declspec(dllimport)
-#endif
-#else
 #define WINPTHREAD_API
 #endif
 
diff --git a/mingw-w64-libraries/winpthreads/include/sched.h 
b/mingw-w64-libraries/winpthreads/include/sched.h
index e77bf4cc3..0b5d7f27b 100644
--- a/mingw-w64-libraries/winpthreads/include/sched.h
+++ b/mingw-w64-libraries/winpthreads/include/sched.h
@@ -49,13 +49,11 @@ struct sched_param {
 extern "C" {
 #endif
 
-#if defined DLL_EXPORT && !defined (WINPTHREAD_EXPORT_ALL_DEBUG)
-#ifdef IN_WINPTHREAD
+/* Declare exported functions as `dllexport` when building the DLL,
+ * but auto-import them outside winpthread.  */
+#if defined(DLL_EXPORT) && !defined(WINPTHREAD_EXPORT_ALL_DEBUG) && 
defined(IN_WINPTHREAD)
 #define WINPTHREAD_SCHED_API __declspec(dllexport)
 #else
-#define WINPTHREAD_SCHED_API __declspec(dllimport)
-#endif
-#else
 #define WINPTHREAD_SCHED_API
 #endif
 
diff --git a/mingw-w64-libraries/winpthreads/include/semaphore.h 
b/mingw-w64-libraries/winpthreads/include/semaphore.h
index 14cb70371..e2790e734 100644
--- a/mingw-w64-libraries/winpthreads/include/semaphore.h
+++ b/mingw-w64-libraries/winpthreads/include/semaphore.h
@@ -27,13 +27,11 @@
 extern "C" {
 #endif
 
-#if defined DLL_EXPORT && !defined (WINPTHREAD_EXPORT_ALL_DEBUG)
-#ifdef IN_WINPTHREAD
+/* Declare exported functions as `dllexport` when building the DLL,
+ * but auto-import them outside winpthread.  */
+#if defined(DLL_EXPORT) && !defined(WINPTHREAD_EXPORT_ALL_DEBUG) && 
defined(IN_WINPTHREAD)
 #define WINPTHREAD_SEMA_API __declspec(dllexport)
 #else
-#define WINPTHREAD_SEMA_API __declspec(dllimport)
-#endif
-#else
 #define WINPTHREAD_SEMA_API
 #endif
 
-- 
2.35.3

Attachment: OpenPGP_signature
Description: OpenPGP digital signature

_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to