GameNetworkingSockets icon indicating copy to clipboard operation
GameNetworkingSockets copied to clipboard

minbase_decls.h always defines DLL_EXPORT on Windows (issues with static pthread)

Open MajorNr01 opened this issue 2 years ago • 0 comments

I build a static version of GameNetworkingSockets for Windows from source along with its dependencies (Protobuf, OpenSSL), using CMake and MinGW on Linux (cross compile). I configure CMake with -DBUILD_STATIC_LIB=ON -DBUILD_SHARED_LIB=OFF.

When using the built library, I try using the -static argument to statically link standard libraries including libwinpthread.

This causes undefined reference errors for symbols such as __imp_pthread_mutex_lock which I have tracked down to the way the preprocessor evaluates pthread.h. In pthread.h the following preprocessor code:

[...]
#if defined DLL_EXPORT
#ifdef IN_WINPTHREAD
#define WINPTHREAD_API __declspec(dllexport)
#else
#define WINPTHREAD_API __declspec(dllimport)
#endif
#else
#define WINPTHREAD_API
#endif
[...]

causes WINPTHREAD_API to evaluate to __declspec(dllimport) because DLL_EXPORT is defined, meaning that declarations such as

int WINPTHREAD_API pthread_mutex_lock(pthread_mutex_t *m);

evaluate to

int __declspec(dllimport) pthread_mutex_lock(pthread_mutex_t *m);

which creates a reference to the symbol __imp_pthread_mutex_lock. I assume that this is some sort of import function for the actual function but I don't exactly know what's going on here.

There are some indications in different versions of pthread.h (e.g. MSYS2 MinGW64) that this version is not entirely correct and that defining DLL_EXPORT should not result in __declspec(dllimport) declarations.

However, I still believe that GameNetworkingSockets should not define DLL_EXPORT when building a static library. Thus far, my only option for using GameNetworkingSockets and statically linking pthread is to insert #undef DLL_EXPORT at an appropriate location in minbase_decls.h.

Still, if there is something going on here that I don't understand, please tell me about it.

MajorNr01 avatar Dec 26 '23 00:12 MajorNr01