brechtsanders / winlibs_mingw

winlibs standalone build of GCC compiler and MinGW-w64
947 stars 44 forks source link

-static with clang++ gives errors about pthreads #241

Open pfmoore opened 2 weeks ago

pfmoore commented 2 weeks ago

Here's a simple reproducible example, using "GCC 14.2.0 (with POSIX threads) + LLVM/Clang/LLD/LLDB 18.1.8 + MinGW-w64 12.0.0 UCRT - release 1" (64-bit version).

hw.cpp:

#include <iostream>

int main() {
    std::cout << "Hello, world!" << std::endl;
    return 0;
}

Compile with clang++ -o hw.exe .\hw.cpp and everything works fine. objdump shows it links at runtime to libstdc++-6.dll.

However, if you try to build a statically linked executable, using clang++ -o hw.exe -static .\hw.cpp the linker thows a series of errors:

C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(locale.o):(.text$__tcf_0+0x8): undefined reference to `pthread_mutex_destroy'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(locale.o):(.text$_ZNSt6locale5facet15_S_get_c_localeEv+0x13): undefined reference to `pthread_once'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(locale.o):(.text.unlikely._ZNSt6locale5_Impl16_M_install_cacheEPKNS_5facetEy+0x54): undefined reference to `pthread_mutex_unlock'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(locale.o):(.text$_ZNSt6locale5_Impl16_M_install_cacheEPKNS_5facetEy+0x2d): undefined reference to `pthread_mutex_lock'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(locale.o):(.text$_ZNSt6locale5_Impl16_M_install_cacheEPKNS_5facetEy+0x17b): undefined reference to `pthread_mutex_unlock'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(locale.o):(.text$_ZNSt6locale5_Impl16_M_install_cacheEPKNS_5facetEy+0x1bc): undefined reference to `pthread_mutex_init'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(locale_init.o):(.text$__tcf_0+0xc): undefined reference to `pthread_mutex_destroy'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(locale_init.o):(.text$_ZN12_GLOBAL__N_116get_locale_mutexEv+0x48): undefined reference to `pthread_mutex_init'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(locale_init.o):(.text$_ZNSt6locale13_S_initializeEv+0x15): undefined reference to `pthread_once'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(locale_init.o):(.text$_ZNSt6locale7classicEv+0x15): undefined reference to `pthread_once'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(locale_init.o):(.text$_ZNSt6localeC2Ev+0x21): undefined reference to `pthread_once'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(locale_init.o):(.text$_ZNSt6localeC2Ev+0x52): undefined reference to `pthread_mutex_lock'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(locale_init.o):(.text$_ZNSt6localeC2Ev+0x6f): undefined reference to `pthread_mutex_unlock'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(locale_init.o):(.text.unlikely._ZNSt6locale6globalERKS_+0x16): undefined reference to `pthread_mutex_unlock'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(locale_init.o):(.text$_ZNSt6locale6globalERKS_+0x1f): undefined reference to `pthread_once'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(locale_init.o):(.text$_ZNSt6locale6globalERKS_+0x41): undefined reference to `pthread_mutex_lock'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(locale_init.o):(.text$_ZNSt6locale6globalERKS_+0xa9): undefined reference to `pthread_mutex_unlock'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(eh_alloc.o):(.text$__tcf_0+0xc): undefined reference to `pthread_mutex_destroy'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(eh_alloc.o):(.text$_ZN12_GLOBAL__N_14pool4freeEPv.constprop.0+0x14): undefined reference to `pthread_mutex_lock'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(eh_alloc.o):(.text$_ZN12_GLOBAL__N_14pool4freeEPv.constprop.0+0xbf): undefined reference to `pthread_mutex_unlock'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(eh_alloc.o):(.text$_ZN12_GLOBAL__N_14pool8allocateEy.constprop.0+0x14): undefined reference to `pthread_mutex_lock'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(eh_alloc.o):(.text$_ZN12_GLOBAL__N_14pool8allocateEy.constprop.0+0x99): undefined reference to `pthread_mutex_unlock'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(eh_alloc.o):(.text.startup._GLOBAL__sub_I__ZN9__gnu_cxx9__freeresEv+0x2a): undefined reference to `pthread_mutex_init'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(guard.o):(.text.unlikely.__cxa_guard_acquire+0x3f): undefined reference to `pthread_mutex_unlock'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(guard.o):(.text$__cxa_guard_acquire+0x45): undefined reference to `pthread_once'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(guard.o):(.text$__cxa_guard_acquire+0x51): undefined reference to `pthread_mutex_lock'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(guard.o):(.text$__cxa_guard_acquire+0x7d): undefined reference to `pthread_once'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(guard.o):(.text$__cxa_guard_acquire+0x8f): undefined reference to `pthread_once'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(guard.o):(.text$__cxa_guard_acquire+0x9e): undefined reference to `pthread_cond_wait'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(guard.o):(.text$__cxa_guard_acquire+0xb7): undefined reference to `pthread_mutex_unlock'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(guard.o):(.text$__cxa_guard_acquire+0xe2): undefined reference to `pthread_mutex_unlock'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(guard.o):(.text$__cxa_guard_abort+0x17): undefined reference to `pthread_once'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(guard.o):(.text$__cxa_guard_abort+0x23): undefined reference to `pthread_mutex_lock'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(guard.o):(.text$__cxa_guard_abort+0x42): undefined reference to `pthread_once'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(guard.o):(.text$__cxa_guard_abort+0x4e): undefined reference to `pthread_cond_broadcast'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(guard.o):(.text$__cxa_guard_abort+0x62): undefined reference to `pthread_mutex_unlock'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(guard.o):(.text$__cxa_guard_release+0x17): undefined reference to `pthread_once'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(guard.o):(.text$__cxa_guard_release+0x23): undefined reference to `pthread_mutex_lock'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(guard.o):(.text$__cxa_guard_release+0x45): undefined reference to `pthread_once'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(guard.o):(.text$__cxa_guard_release+0x51): undefined reference to `pthread_cond_broadcast'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib\libstdc++.a(guard.o):(.text$__cxa_guard_release+0x65): undefined reference to `pthread_mutex_unlock'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib/gcc/x86_64-w64-mingw32/14.2.0\libgcc_eh.a(emutls.o): in function `__gthread_mutex_init_function':
R:/winlibs64ucrt_stage/gcc-14.2.0/build_mingw/x86_64-w64-mingw32/libgcc/./gthr-default.h:746:(.text+0x5e): undefined reference to `pthread_mutex_init'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib/gcc/x86_64-w64-mingw32/14.2.0\libgcc_eh.a(emutls.o): in function `__gthread_key_create':
R:/winlibs64ucrt_stage/gcc-14.2.0/build_mingw/x86_64-w64-mingw32/libgcc/./gthr-default.h:721:(.text+0x71): undefined reference to `pthread_key_create'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib/gcc/x86_64-w64-mingw32/14.2.0\libgcc_eh.a(emutls.o): in function `__gthread_getspecific':
R:/winlibs64ucrt_stage/gcc-14.2.0/build_mingw/x86_64-w64-mingw32/libgcc/./gthr-default.h:733:(.text+0xad): undefined reference to `pthread_getspecific'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib/gcc/x86_64-w64-mingw32/14.2.0\libgcc_eh.a(emutls.o): in function `__gthread_once':
R:/winlibs64ucrt_stage/gcc-14.2.0/build_mingw/x86_64-w64-mingw32/libgcc/./gthr-default.h:713:(.text+0xf7): undefined reference to `pthread_once'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib/gcc/x86_64-w64-mingw32/14.2.0\libgcc_eh.a(emutls.o): in function `__gthread_mutex_lock':
R:/winlibs64ucrt_stage/gcc-14.2.0/build_mingw/x86_64-w64-mingw32/libgcc/./gthr-default.h:762:(.text+0x106): undefined reference to `pthread_mutex_lock'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib/gcc/x86_64-w64-mingw32/14.2.0\libgcc_eh.a(emutls.o): in function `__gthread_mutex_unlock':
R:/winlibs64ucrt_stage/gcc-14.2.0/build_mingw/x86_64-w64-mingw32/libgcc/./gthr-default.h:792:(.text+0x11b): undefined reference to `pthread_mutex_unlock'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/lib/gcc/x86_64-w64-mingw32/14.2.0\libgcc_eh.a(emutls.o): in function `__gthread_setspecific':
R:/winlibs64ucrt_stage/gcc-14.2.0/build_mingw/x86_64-w64-mingw32/libgcc/./gthr-default.h:739:(.text+0x157): undefined reference to `pthread_setspecific'
C:/Users/Gustav/scoop/apps/mingw-winlibs-llvm-ucrt/current/bin/ld: R:/winlibs64ucrt_stage/gcc-14.2.0/build_mingw/x86_64-w64-mingw32/libgcc/./gthr-default.h:739:(.text+0x220): undefined reference to `pthread_setspecific'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)

This is particularly frustrating, as I have no need of threads. And it's worth noting that the non-static build did not link to libwinpthread-1.dll, so the compiler obviously recognises that there's no need for thread support.

brechtsanders commented 2 weeks ago

The winlibs attempt to build LLVM uses the GCC standard libraries in order to allow both toolkits to be used interchangeable. However this is probably not a good idea.

LLVM comes with its own standard libraries and I would really recommend using the builds from https://github.com/mstorsjo/llvm-mingw

Alcaro commented 2 weeks ago

If you recommend not using them, perhaps you should document them as such on winlibs.com.

And put the without-clang builds before with-clang.

pfmoore commented 2 weeks ago

Thanks. I was using this because it's really hard to find a good build of LLVM for Windows. The ones from the LLVM site use the MSVC standard C library, and broke horribly because I have a project that needs C++11, and the MSVC libs no longer support C++11. So I picked the winlibs build because I was already using it for gcc.

I'll look into the mstorsjo builds as an alternative, and switch to the without-clang builds from here.

morrowwolf commented 5 days ago

The winlibs attempt to build LLVM uses the GCC standard libraries in order to allow both toolkits to be used interchangeable. However this is probably not a good idea.

LLVM comes with its own standard libraries and I would really recommend using the builds from https://github.com/mstorsjo/llvm-mingw

Oof, unless I just missed it (and it's possible) it'd be really nice to have this stated up front on releases.