On Sun, 1 May 2022, JonY via Mingw-w64-public wrote:

I think dllexport should be replaced by dllimport in the above statement.


Yes it was a typo. Revised patch attached.

On Sun, May 1, 2022 at 9:36 AM Martin Storsjö <[email protected]> wrote:


FWIW, I think this will break a number of users, who currently
successfully are linking statically, who now need to set a winpthread
specific define to make it work. Yes, they can change their setups to
manually define WINPTHREAD_STATIC, but it will cause lots of extra
inconvenience for users with setups that so far have worked just fine.

But if you really really prefer this setup, then fine, go ahead. But I did
warn that it will inconvenirnce users.


That concerns me too. I still prefer the previous patch which removes `dllimport` entirely, because it is consistent between EXEs and DLLs and is less verbose, unless there are bugs about the auto-import feature.

I am unclear about how others build DLLs on mingw-w64. Personally I just export all functions so there is no dllexport or dllimport whatsoever.



在 2022-05-01 15:55, Vincent Torri 写道:

should winpthread be used by users ? i thought it was written for c++11 threads


Yes there are facilities that pthread provide while the standard c++ library doesn't, such as `pthread_barrier_t`. There are also C libraries that call pthread APIs directly, such as GOMP.

C++11 threads can also be implement without pthreads. I happen to have one called mcfgthread which you can find on GitHub. libc++ by llvm people also implements it directly with Windows APIs.



--
Best regards,
LIU Hao
From d4cdd80536d3cfa01a8522affd5fd61df266aeb5 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.

This commit addresses this issue by introducing the `WINPTHREAD_STATIC`
macro that prevents `dllimport` attributes when told so.

Reference: https://sourceforge.net/p/mingw-w64/mailman/message/37647350/
Signed-off-by: LIU Hao <[email protected]>
---
 mingw-w64-libraries/winpthreads/README           | 10 ++++++++++
 .../winpthreads/include/pthread.h                | 16 ++++++++++------
 mingw-w64-libraries/winpthreads/include/sched.h  | 16 ++++++++++------
 .../winpthreads/include/semaphore.h              | 16 ++++++++++------
 4 files changed, 40 insertions(+), 18 deletions(-)
 create mode 100644 mingw-w64-libraries/winpthreads/README

diff --git a/mingw-w64-libraries/winpthreads/README 
b/mingw-w64-libraries/winpthreads/README
new file mode 100644
index 000000000..50d8dbe9b
--- /dev/null
+++ b/mingw-w64-libraries/winpthreads/README
@@ -0,0 +1,10 @@
+The Winpthreads Library
+-----------------------
+
+This library provides POSIX threading APIs for mingw-w64.
+
+Programs are usually linked against the winpthreads DLL, and winpthreads
+headers expose `dllimport` APIs by default. When linking against the
+static library, especially when building a user DLL with libtool, it is
+necessary to define the `WINPTHREAD_STATIC` macro to avoid undefined
+references.
diff --git a/mingw-w64-libraries/winpthreads/include/pthread.h 
b/mingw-w64-libraries/winpthreads/include/pthread.h
index 5aadb1cfa..d17df3352 100644
--- a/mingw-w64-libraries/winpthreads/include/pthread.h
+++ b/mingw-w64-libraries/winpthreads/include/pthread.h
@@ -83,14 +83,18 @@ 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
-#define WINPTHREAD_API __declspec(dllexport)
+#  ifdef DLL_EXPORT
+#    define WINPTHREAD_API __declspec(dllexport)
+#  else
+#    define WINPTHREAD_API
+#  endif
 #else
-#define WINPTHREAD_API __declspec(dllimport)
-#endif
-#else
-#define WINPTHREAD_API
+#  ifdef WINPTHREAD_STATIC
+#    define WINPTHREAD_API
+#  else
+#    define WINPTHREAD_API __declspec(dllimport)
+#  endif
 #endif
 
 /* #define WINPTHREAD_DBG 1 */
diff --git a/mingw-w64-libraries/winpthreads/include/sched.h 
b/mingw-w64-libraries/winpthreads/include/sched.h
index e77bf4cc3..2f6ce7c49 100644
--- a/mingw-w64-libraries/winpthreads/include/sched.h
+++ b/mingw-w64-libraries/winpthreads/include/sched.h
@@ -49,14 +49,18 @@ struct sched_param {
 extern "C" {
 #endif
 
-#if defined DLL_EXPORT && !defined (WINPTHREAD_EXPORT_ALL_DEBUG)
 #ifdef IN_WINPTHREAD
-#define WINPTHREAD_SCHED_API __declspec(dllexport)
+#  if defined(DLL_EXPORT) && !defined(WINPTHREAD_EXPORT_ALL_DEBUG)
+#    define WINPTHREAD_SCHED_API __declspec(dllexport)
+#  else
+#    define WINPTHREAD_SCHED_API
+#  endif
 #else
-#define WINPTHREAD_SCHED_API __declspec(dllimport)
-#endif
-#else
-#define WINPTHREAD_SCHED_API
+#  ifdef WINPTHREAD_STATIC
+#    define WINPTHREAD_SCHED_API
+#  else
+#    define WINPTHREAD_SCHED_API __declspec(dllimport)
+#  endif
 #endif
 
 int WINPTHREAD_SCHED_API sched_yield(void);
diff --git a/mingw-w64-libraries/winpthreads/include/semaphore.h 
b/mingw-w64-libraries/winpthreads/include/semaphore.h
index 14cb70371..fb08d58d7 100644
--- a/mingw-w64-libraries/winpthreads/include/semaphore.h
+++ b/mingw-w64-libraries/winpthreads/include/semaphore.h
@@ -27,14 +27,18 @@
 extern "C" {
 #endif
 
-#if defined DLL_EXPORT && !defined (WINPTHREAD_EXPORT_ALL_DEBUG)
 #ifdef IN_WINPTHREAD
-#define WINPTHREAD_SEMA_API __declspec(dllexport)
+#  if defined(DLL_EXPORT) && !defined(WINPTHREAD_EXPORT_ALL_DEBUG)
+#    define WINPTHREAD_SEMA_API __declspec(dllexport)
+#  else
+#    define WINPTHREAD_SEMA_API
+#  endif
 #else
-#define WINPTHREAD_SEMA_API __declspec(dllimport)
-#endif
-#else
-#define WINPTHREAD_SEMA_API
+#  ifdef WINPTHREAD_STATIC
+#    define WINPTHREAD_SEMA_API
+#  else
+#    define WINPTHREAD_SEMA_API __declspec(dllimport)
+#  endif
 #endif
 
 /* Set this to 0 to disable it */
-- 
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