google / googletest

GoogleTest - Google Testing and Mocking Framework
https://google.github.io/googletest/
BSD 3-Clause "New" or "Revised" License
34.76k stars 10.15k forks source link

Googletest does not compile with POSIX Threads for Windows #3577

Open ingo-h opened 3 years ago

ingo-h commented 3 years ago

Describe the bug

pthreads4w is installed and working. I can compile programs with it. If I build googletest with default settings it does not detect pthread. pthreads4w can only be compiled with Generator "NMake Makefiles". If I try to also compile googletest with that Generator build fails with a bunch of error messages.

I expect that configuration and build of googletest detect third party pthread installation on Microsoft Windows and successful compile it with default Generator "Visual Studio *" and with Generator "NMake Makefiles" and then use POSIX threads.

Steps to reproduce the bug

First Install third party POSIX Threads for Windows. Then configure googletest with default settings using PowerShell:

PS> cmake -S . -B build
-- Building for: Visual Studio 16 2019
-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19042.
-- The C compiler identification is MSVC 19.28.29915.0
-- The CXX compiler identification is MSVC 19.28.29915.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.28.29910/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.28.29910/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Python: C:/Users/ingo/AppData/Local/Programs/Python/Python39/python.exe (found version "3.9.7") found components: Interpreter
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - not found
-- Check if compiler accepts -pthread
-- Check if compiler accepts -pthread - no
-- Found Threads: TRUE
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/ingo/devel/googletest/build

As shown header files are found but pthread isn't detected. Following build of googletest works but without multithreading. If Configuring with Generator "NMake Makefiles" I get:

PS> rm -R build
PS> cmake -S . -B build -G "NMake Makefiles"
-- The C compiler identification is MSVC 19.28.29915.0
-- The CXX compiler identification is MSVC 19.28.29915.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.28.29910/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.28.29910/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Python: C:/Users/ingo/AppData/Local/Programs/Python/Python39/python.exe (found version "3.9.7") found components: Interpreter
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/ingo/devel/googletest/build

Looks good so far. But with building I get:

PS> cmake --build build
Scanning dependencies of target gmock_main
[  9%] Building CXX object googlemock/CMakeFiles/gmock_main.dir/__/googletest/src/gtest-all.cc.objgtest-all.cc
C:\Users\ingo\devel\googletest\googletest\include\gtest/internal/gtest-port.h(1173): error C3861: 'nanosleep': identifier not found
C:\Users\ingo\devel\googletest\googletest\include\gtest/internal/gtest-port.h(1488): error C2011: 'testing::internal::ThreadWithParamBase': 'class' type redefinition
C:\Users\ingo\devel\googletest\googletest\include\gtest/internal/gtest-port.h(1287): note: see declaration of 'testing::internal::ThreadWithParamBase'
C:\Users\ingo\devel\googletest\googletest\include\gtest/internal/gtest-port.h(1537): error C2953: 'testing::internal::ThreadWithParam': class template has already been defined
C:\Users\ingo\devel\googletest\googletest\include\gtest/internal/gtest-port.h(1508): note: see declaration of 'testing::internal::ThreadWithParam'
C:\Users\ingo\devel\googletest\googletest\src/gtest-death-test.cc(671): error C3646: 'write_handle_': unknown override specifier
C:\Users\ingo\devel\googletest\googletest\src/gtest-death-test.cc(671): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
C:\Users\ingo\devel\googletest\googletest\src/gtest-death-test.cc(673): error C3646: 'child_handle_': unknown override specifier
C:\Users\ingo\devel\googletest\googletest\src/gtest-death-test.cc(673): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
C:\Users\ingo\devel\googletest\googletest\src/gtest-death-test.cc(678): error C3646: 'event_handle_': unknown override specifier
C:\Users\ingo\devel\googletest\googletest\src/gtest-death-test.cc(678): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
C:\Users\ingo\devel\googletest\googletest\src/gtest-death-test.cc(690): error C2065: 'child_handle_': undeclared identifier
C:\Users\ingo\devel\googletest\googletest\src/gtest-death-test.cc(690): error C2065: 'event_handle_': undeclared identifier
C:\Users\ingo\devel\googletest\googletest\src/gtest-death-test.cc(704): error C2065: 'write_handle_': undeclared identifier
C:\Users\ingo\devel\googletest\googletest\src/gtest-death-test.cc(705): error C2065: 'event_handle_': undeclared identifier
C:\Users\ingo\devel\googletest\googletest\src/gtest-death-test.cc(713): error C2065: 'child_handle_': undeclared identifier
C:\Users\ingo\devel\googletest\googletest\src/gtest-death-test.cc(717): error C2065: 'child_handle_': undeclared identifier
C:\Users\ingo\devel\googletest\googletest\src/gtest-death-test.cc(719): error C2065: 'child_handle_': undeclared identifier
C:\Users\ingo\devel\googletest\googletest\src/gtest-death-test.cc(754): error C2065: 'write_handle_': undeclared identifier
C:\Users\ingo\devel\googletest\googletest\src/gtest-death-test.cc(755): error C2065: 'event_handle_': undeclared identifier
C:\Users\ingo\devel\googletest\googletest\src/gtest-death-test.cc(760): error C2065: 'event_handle_': undeclared identifier
C:\Users\ingo\devel\googletest\googletest\src/gtest-death-test.cc(773): error C2065: 'event_handle_': undeclared identifier
C:\Users\ingo\devel\googletest\googletest\src/gtest-death-test.cc(773): error C2672: 'testing::internal::StreamableToString': no matching overloaded function found
    --- snip ---
    ----snap ---
NMAKE : fatal error U1077: 'C:\PROGRA~2\MICROS~2\2019\COMMUN~1\VC\Tools\MSVC\1428~1.299\bin\Hostx64\x64\cl.exe' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29910\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29910\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.

Does the bug persist in the most recent commit? Yes.

What operating system and version are you using?

PS> Get-ComputerInfo -Property Windows*
WindowsBuildLabEx              : 19041.1.amd64fre.vb_release.191206-1406
WindowsCurrentVersion          : 6.3
WindowsEditionId               : Core
WindowsInstallationType        : Client
WindowsInstallDateFromRegistry : 03/05/2021 22:59:12
WindowsProductName             : Windows 10 Home
WindowsRegisteredOrganization  :
WindowsRegisteredOwner         : ingo
WindowsSystemRoot              : C:\WINDOWS
WindowsVersion                 : 2009
WindowsUBR                     : 1237

From a cmd prompt:

"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" amd64
**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.9.5
** Copyright (c) 2021 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'

What compiler and version are you using?

PS> cl
Microsoft (R) C/C++ Optimizing Compiler Version 19.28.29915 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

cl : Command line error D8003 : missing source filename>

What build system are you using?

PS> cmake --version
cmake version 3.19.20122902-MSVC_2

CMake suite maintained and supported by Kitware (kitware.com/cmake).
juniperfdel commented 2 years ago

I had this same issue and it turned out that two preprocesser macros were conflicting with one another. Specifically, these two macros in gtest-port.h both returned true and defined the class testing::internal::ThreadWithParamBase.

https://github.com/google/googletest/blob/25ad42aabeaba6227f37795cdd2752e128e83827/googletest/include/gtest/internal/gtest-port.h#L1253

https://github.com/google/googletest/blob/25ad42aabeaba6227f37795cdd2752e128e83827/googletest/include/gtest/internal/gtest-port.h#L1338

I ended up fixing it by changing !GTEST_OS_WINDOWS_MINGW to !GTEST_OS_WINDOWS in the first #if statement on line 1253 to have the preprocessor only select line 1338 as true.