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

Fix compilation of googletest with MinGW using Win32 threads #721

Merged
merged 2 commits into from
Aug 24, 2016

Conversation

ilmagico
Copy link
Contributor

Fixes #708.

Allows to compile googletest with MinGW when pthreads support is disabled.
Also, the cmake build automatically disables pthreads supportwhen MinGW is used.

Tested on Win7, with MinGW 4.8.1, cmake 3.5.0. All tests pass, except for gmock-matchers_test that fails because of a limitation of MinGW not being able to handle "too many sections", most likely unrelated to the issue being addressed here (see below).

Unfortunately, I am not able to (legally) test with MSVC compiler, anybody please help test.

Also tested on Linux, although I expect no differences.

cmake command line used for testing:
cmake -G "MinGW Makefiles" -Dgtest_build_samples=ON -Dgmock_build_samples=ON -Dgtest_build_tests=ON -Dgmock_build_tests=ON ..

MinGW "too many section" issue:

c:/mingw-2/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/as.exe: CMakeFiles\gmock-matchers_test.dir\test\gmock-matchers_test.cc.obj: too many sections (43319)
C:\Users\ilmagico\AppData\Local\Temp\cc4PXqrl.s: Assembler messages:
C:\Users\ilmagico\AppData\Local\Temp\cc4PXqrl.s: Fatal error: can't write CMakeFiles\gmock-matchers_test.dir\test\gmock-matchers_test.cc.obj: File too big
c:/mingw-2/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/as.exe: CMakeFiles\gmock-matchers_test.dir\test\gmock-matchers_test.cc.obj: too many sections (43319)
C:\Users\ilmagico\AppData\Local\Temp\cc4PXqrl.s: Fatal error: can't close CMakeFiles\gmock-matchers_test.dir\test\gmock-matchers_test.cc.obj: File too big

and (after using make --keep-going)

98% tests passed, 1 tests failed out of 59

Total Test time (real) =  26.45 sec

The following tests FAILED:
          9 - gmock-matchers_test (Not Run)

Apparently this limitation is lifted in recent versions of MinGW64 (when compiling in 64-bit mode), if I get a chance I'll try that too.

It's not supported, and native Windows threading is available for MinGW
#if GTEST_OS_WINDOWS_MINGW
// MinGW defined _CRITICAL_SECTION and _RTL_CRITICAL_SECTION as two
// separate (equivalent) structs, instead of using typedef
typedef struct _CRITICAL_SECTION GTEST_CRITICAL_SECTION;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks compilation with MinGW 5.2.0.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for me. This breaks compilation on Linux with MinGW 4.8.2

gtest-port.cc:242:45: error: cannot convert ‘CRITICAL_SECTION* {aka _RTL_CRITICAL_SECTION*}’ to ‘GTEST_CRITICAL_SECTION* {aka _CRITICAL_SECTION*}’ in initialization
       critical_section_(new CRITICAL_SECTION) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix this in PR #856, but a lot of test still failing with MinGW

@orgads
Copy link
Contributor

orgads commented Mar 20, 2016

Tested with MSVC. All compiles. With recent MinGW it is broken though.

@KindDragon KindDragon mentioned this pull request Mar 29, 2016
Nikolai-Hlubek added a commit to Nikolai-Hlubek/googletest that referenced this pull request Jul 4, 2016
Simple fix for the compile issues (google#484, google#708) with MinGW 4.8.1. 

The real fix is under discussion in google#721.
@andoks
Copy link

andoks commented Aug 2, 2016

Seems to work with MSVC 2010. But fails for me when trying to build with MinGW (g++ -v: gcc version 4.9.2 (i686-posix-dwarf-rev1, Built by MinGW-W64 project) )

cmake -G "MSYS Makefiles" -H. -B_builds -DCMAKE_INSTALL_PREFIX=$(pwd)/_install
cmake --build _builds/ --target install

First error is:

googletest-fix-mingw-threads/googletest/src/gtest-port.cc:242:45: error: cannot convert 'CRITICAL_SECTION* {aka RTL_CRITICAL_SECTION}' to 'GTEST_CRITICAL_SECTION_ {aka CRITICAL_SECTION*}' in initialization
critical_section
(new CRITICAL_SECTION) {

@andoks
Copy link

andoks commented Aug 2, 2016

I believe I was experiencing the same issue late last year (see issue #606 and PR #608).

As far as I can understand the original issue is that commit a634042 introduced internal testing of GTests' use of threads, and tried to disable pthreads on MinGW but failed.

I tried building GTest master with something similar to PR #608 above, and disabling pthreads on a top-level makes the build work on MinGW.

Maybe the best way to go about it would be to first properly disable pthreads, then implement the threading testing when building with MinGW?

@BillyDonahue
Copy link
Contributor

This looks good. Thanks.

@BillyDonahue BillyDonahue merged commit ed9d1e1 into google:master Aug 24, 2016
@KindDragon KindDragon mentioned this pull request Aug 24, 2016
@ArekPiekarz
Copy link

ArekPiekarz commented Aug 29, 2016

This merge broke compilation on Windows 10 x64 with MinGW 5.3 x32. It comes bundled with Qt 5.7 SDK. Now I get errors like this:

In file included from ......\lib\googletest\googletest\src\gtest-all.cc:45:0:
D:/dev/lib/googletest/googletest/src/gtest-port.cc: In constructor 'testing::internal::Mutex::Mutex()':
D:/dev/lib/googletest/googletest/src/gtest-port.cc:242:45: error: cannot convert 'CRITICAL_SECTION* {aka RTL_CRITICAL_SECTION}' to 'GTEST_CRITICAL_SECTION_ {aka CRITICAL_SECTION*}' in initialization
critical_section
(new CRITICAL_SECTION) {
^
D:/dev/lib/googletest/googletest/src/gtest-port.cc:243:48: error: cannot convert 'GTEST_CRITICAL_SECTION* {aka CRITICAL_SECTION}' to 'LPCRITICAL_SECTION {aka RTL_CRITICAL_SECTION}' for argument '1' to 'void InitializeCriticalSection(LPCRITICAL_SECTION)'
::InitializeCriticalSection(critical_section_);
^
In file included from D:/dev/lib/googletest/googletest/include/gtest/internal/gtest-internal.h:40:0,
from D:/dev/lib/googletest/googletest/include/gtest/gtest.h:58,
from ......\lib\googletest\googletest\src\gtest-all.cc:39:
D:/dev/lib/googletest/googletest/include/gtest/internal/gtest-port.h:402:16: note: class type 'GTEST_CRITICAL_SECTION {aka _CRITICAL_SECTION}' is incomplete
typedef struct CRITICAL_SECTION GTEST_CRITICAL_SECTION;
^
In file included from ......\lib\googletest\googletest\src\gtest-all.cc:45:0:
D:/dev/lib/googletest/googletest/src/gtest-port.cc: In destructor 'testing::internal::Mutex::~Mutex()':
D:/dev/lib/googletest/googletest/src/gtest-port.cc:253:46: error: cannot convert 'GTEST_CRITICAL_SECTION* {aka CRITICAL_SECTION}' to 'LPCRITICAL_SECTION {aka RTL_CRITICAL_SECTION}' for argument '1' to 'void DeleteCriticalSection(LPCRITICAL_SECTION)'
::DeleteCriticalSection(critical_section
);
^

@evgen48 evgen48 mentioned this pull request Sep 1, 2016
zrax added a commit to zrax/string_theory that referenced this pull request Jan 11, 2017
msk-repo01 added a commit to msk-repo01/opencv that referenced this pull request Feb 2, 2017
fix for opencv#8105, compilation issue with mingw32 (in
google/googletest#721 a similar issue was solved and the reason was
described as MinGW defines _CRITICAL_SECTION and _RTL_CRITICAL_SECTION
as two separate (equivalent) structs, instead of using typedef)
@SylvainCorlay SylvainCorlay mentioned this pull request Jul 5, 2017
dneto0 added a commit to dneto0/shaderc that referenced this pull request Aug 9, 2023
When compiling with MinGW, we no longer need to use
-Dgtest_disable_pthreads=ON.

See google/googletest#856
and google/googletest#721
dneto0 added a commit to dneto0/shaderc that referenced this pull request Aug 9, 2023
When compiling with MinGW, we no longer need to use
-Dgtest_disable_pthreads=ON.

See google/googletest#856
and google/googletest#721
dneto0 added a commit to google/shaderc that referenced this pull request Aug 9, 2023
When compiling with MinGW, we no longer need to use
-Dgtest_disable_pthreads=ON.

See google/googletest#856
and google/googletest#721
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

GTest not compiling
7 participants