abseil / abseil-cpp

Abseil Common Libraries (C++)
https://abseil.io
Apache License 2.0
14.91k stars 2.61k forks source link

Building Abseil 20230802.0 with MinGW-w64 fails #1510

Open brechtsanders opened 1 year ago

brechtsanders commented 1 year ago

Describe the issue

Building Abseil 20230802.0 with MinGW-w64 fails when building with -DBUILD_SHARED_LIBS:BOOL=ON

[213/236] Linking CXX shared library bin\libabsl_synchronization.dll
FAILED: bin/libabsl_synchronization.dll absl/synchronization/libabsl_synchronization.dll.a
cmd.exe /C "cd . && D:\Prog\winlibs64-13.1.0msvcrt\mingw64\bin\x86_64-w64-mingw32-g++.exe -std=c++17 -O3 -DNDEBUG   -shared -o bin\libabsl_synchronization.dll -Wl,--out-implib,absl\synchronization\libabsl_synchronization.dll.a -Wl,--major-image-version,0,--minor-image-version,0 absl/synchronization/CMakeFiles/synchronization.dir/barrier.cc.obj absl/synchronization/CMakeFiles/synchronization.dir/blocking_counter.cc.obj absl/synchronization/CMakeFiles/synchronization.dir/internal/create_thread_identity.cc.obj absl/synchronization/CMakeFiles/synchronization.dir/internal/futex_waiter.cc.obj absl/synchronization/CMakeFiles/synchronization.dir/internal/per_thread_sem.cc.obj absl/synchronization/CMakeFiles/synchronization.dir/internal/pthread_waiter.cc.obj absl/synchronization/CMakeFiles/synchronization.dir/internal/sem_waiter.cc.obj absl/synchronization/CMakeFiles/synchronization.dir/internal/stdcpp_waiter.cc.obj absl/synchronization/CMakeFiles/synchronization.dir/internal/waiter_base.cc.obj absl/synchronization/CMakeFiles/synchronization.dir/internal/win32_waiter.cc.obj absl/synchronization/CMakeFiles/synchronization.dir/notification.cc.obj absl/synchronization/CMakeFiles/synchronization.dir/mutex.cc.obj  absl/synchronization/libabsl_graphcycles_internal.dll.a  absl/synchronization/libabsl_kernel_timeout_internal.dll.a  absl/debugging/libabsl_stacktrace.dll.a  absl/debugging/libabsl_symbolize.dll.a  absl/time/libabsl_time.dll.a  absl/time/libabsl_civil_time.dll.a  absl/time/libabsl_time_zone.dll.a  absl/base/libabsl_malloc_internal.dll.a  absl/debugging/libabsl_debugging_internal.dll.a  absl/debugging/libabsl_demangle_internal.dll.a  absl/strings/libabsl_strings.dll.a  absl/numeric/libabsl_int128.dll.a  absl/strings/libabsl_string_view.dll.a  absl/strings/libabsl_strings_internal.dll.a  absl/base/libabsl_base.dll.a  -lpthread  absl/base/libabsl_spinlock_wait.dll.a  absl/base/libabsl_throw_delegate.dll.a  absl/base/libabsl_raw_logging_internal.dll.a  absl/base/libabsl_log_severity.dll.a  -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 -pthread && cd ."
D:/Prog/winlibs64-13.1.0msvcrt/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: absl/synchronization/CMakeFiles/synchronization.dir/internal/per_thread_sem.cc.obj:per_thread_sem:(.text+0x14b): undefined reference to `absl::lts_20230802::synchronization_internal::Win32Waiter::Wait(absl::lts_20230802::synchronization_internal::KernelTimeout)'
D:/Prog/winlibs64-13.1.0msvcrt/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: absl/synchronization/CMakeFiles/synchronization.dir/internal/per_thread_sem.cc.obj:per_thread_sem:(.text+0xb5): undefined reference to `absl::lts_20230802::synchronization_internal::Win32Waiter::Poke()'
D:/Prog/winlibs64-13.1.0msvcrt/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: absl/synchronization/CMakeFiles/synchronization.dir/internal/per_thread_sem.cc.obj:per_thread_sem:(.text+0xc5): undefined reference to `absl::lts_20230802::synchronization_internal::Win32Waiter::Win32Waiter()'
D:/Prog/winlibs64-13.1.0msvcrt/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: absl/synchronization/CMakeFiles/synchronization.dir/internal/per_thread_sem.cc.obj:per_thread_sem:(.text+0xd5): undefined reference to `absl::lts_20230802::synchronization_internal::Win32Waiter::Post()'
D:/Prog/winlibs64-13.1.0msvcrt/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: absl/synchronization/CMakeFiles/synchronization.dir/internal/per_thread_sem.cc.obj:per_thread_sem:(.text+0xe5): undefined reference to `absl::lts_20230802::synchronization_internal::Win32Waiter::Poke()'
collect2.exe: error: ld returned 1 exit status
[214/236] Building CXX object absl/strings/CMakeFiles/cord.dir/cord.cc.obj
ninja: build stopped: subcommand failed.

Steps to reproduce the problem

cmake.exe -GNinja -DCMAKE_BUILD_TYPE:STRING=Release -DBUILD_SHARED_LIBS:BOOL=ON -DABSL_USE_GOOGLETEST_HEAD:BOOL=OFF -DABSL_RUN_TESTS:BOOL=OFF -DCMAKE_CXX_FLAGS:STRING="-std=c++17" -S. -Bbuild_shared
ninja -Cbuild_shared

What version of Abseil are you using?

20230802.0

What operating system and version are you using?

Windows 11

What compiler and version are you using?

Tried with GCC 12.2.0 and 13.1.0

What build system are you using?

CMake 3.27.1 + Ninja 1.11.1

Additional context

Builds fine with -DBUILD_SHARED_LIBS:BOOL=OFF

flxzt commented 1 year ago

This happens for me as well in a project that uses absl, but disabling building shared libs did not help.

Forcing a specific implementation of abseil waiter with cxxflag -DABSL_FORCE_WAITER_MODE=4 (4 for stdcpp) seems to be a work-around for the build errors related to Waiter. I found this option in waiter.h.

brechtsanders commented 1 year ago

For me adding -DABSL_FORCE_WAITER_MODE=4 (4 = ABSL_WAITER_MODE_STDCPP) to CMake flag CMAKE_CXX_FLAGS also fixes the build.

But I think it would be cleaner to actually fix the native method (3 = ABSL_WAITER_MODE_WIN32).

derekmauro commented 1 year ago

I tried this on Linux using mingw32 as a cross compiler and saw what @flxzt mentioned. I think it is a different issue from @brechtsanders original report.

As far as I can tell, the native implementation requires at least Windows Vista. When I tried mingw32 as a cross compiler, it seems to by default target an earlier version of Windows, which is why the build fails. Maybe there is some flag to specify the minimum Windows version to target? At head we now fallback to StdcppWaiter, which fixes this issue.

The original report from @brechtsanders seems to be trying to use the native implementation. I haven't tried to figure out why it is failing yet, but it does appear to fail in a different way.