Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Googletest does not compile with POSIX Threads for Windows #3577

Open
ingo-h opened this issue Sep 22, 2021 · 1 comment
Open

Googletest does not compile with POSIX Threads for Windows #3577

ingo-h opened this issue Sep 22, 2021 · 1 comment
Assignees
Labels

Comments

@ingo-h
Copy link

ingo-h commented Sep 22, 2021

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).
@ingo-h ingo-h added the bug label Sep 22, 2021
ingo-h added a commit to upnplib/upnplib that referenced this issue Sep 26, 2021
Make Unit Tests running with the new GoogleTest Installation and found
that GoogleMock segfaults if compiled without PThreads. I tried it
because it is necessary on MS Windows to successfully compile there. See
bug report google/googletest#3577.

The bad news, we cannot setup consistently:
- we MUST GoogleTest compile with pthread on Linux
- we MUST GoogleTest compile without pthreads4w on MS Windows
to be able to use mocking.
ingo-h added a commit to upnplib/upnplib that referenced this issue Sep 26, 2021
Make Unit Tests running with the new GoogleTest Installation and found
that GoogleMock segfaults if compiled without PThreads. I tried it
because it is necessary on MS Windows to successfully compile there. See
bug report google/googletest#3577.

The bad news, we cannot setup consistently:
- we MUST GoogleTest compile with pthread on Linux
- we MUST GoogleTest compile without pthreads4w on MS Windows
to be able to use mocking.
@derekmauro derekmauro self-assigned this Sep 27, 2021
@juniperfdel
Copy link

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.

# if GTEST_HAS_PTHREAD && !GTEST_OS_WINDOWS_MINGW

# elif GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT

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.

hobu added a commit to PDAL/PDAL that referenced this issue Feb 12, 2023
google/googletest#3577 (comment)

Weird pthread interaction in gtest in some windows builds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants