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

Windows (VS) + cmake + googletest problems - need help #3218

Closed
melh23 opened this issue Jan 12, 2021 · 2 comments
Closed

Windows (VS) + cmake + googletest problems - need help #3218

melh23 opened this issue Jan 12, 2021 · 2 comments

Comments

@melh23
Copy link

melh23 commented Jan 12, 2021

Hello,
I’m a TA using googeltest in Github classroom. We have a setup with cmake that works on my Mac that will produce a makefile and runs google tests successfully. These projects are graded, and the Google tests “PASS” on github classroom when pushed.

The problem is things do not go as smoothly on Windows machines. While we can create a new solution on VS and compile and run these test files, it is not easily possible to push them to Github classroom to be autograded. We think having the same workflow as Github classroom autograder would be easiest to complete projects:

cmake.. && make && ./bin/test_executable 

Students on Windows machines are able to run a project with the cmake workflow, but are unable to compile and run the googletests. When they try to run the project with cmake workflow on Windows, there are errors only when building the executables for the googletest files.

The commands we are using to build the project (in a build folder) are:

cmake -G “MinGW Makefiles” ..	// this runs without errors
make

The make command produces a main.exe for the main executable that does not use the google test framework, but not for either of the 2 googletest files.

Things we have tried:

  • tutorials online for running gtest on windows but have received similar errors.
  • Looking at Issue 606
  • Have tried disabling the pthreads, and various solutions suggested in the previously mentioned issue, but never got past these errors.

Questions:

  1. How can we get executables for my googletest files on Windows?
  2. Is it even possible to generate executables from googletest .cpp files on Windows?
  3. Are there any alternatives to compiling googletests on Windows short of a Virtual Machine running another OS?

Here is the CMakeLists.txt:

project(rational_num-melh23 CXX)

# CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

# ------------------------------------
# place binaries and libraries according to GNU standards
include(GNUInstallDirs)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})
if(CMAKE_CXX_COMPILER_ID MATCHES GNU)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
endif()
#. . . . . . . . . . . . . . . . . . . .
include(cmake/googletest.cmake)
fetch_googletest(
    ${PROJECT_SOURCE_DIR}/cmake
    ${PROJECT_BINARY_DIR}/googletest
    )

# ADD_SUBDIRECTORY(googletest)

ADD_EXECUTABLE(main
    main.cpp
    !test_includes/random.cpp
    !test_includes/utilities.cpp

    #- - - - - - - - - - - - - - - - 
    # cpp files we are testing (no commas)
    rational.cpp
    #- - - - - - - - - - - - - - - - 
)

ADD_EXECUTABLE(testA
    !test_files/testA.cpp
    !test_includes/random.cpp
    !test_includes/utilities.cpp
    #- - - - - - - - - - - - - - - - 
    # cpp files we are testing (no commas)
    rational.cpp
    #- - - - - - - - - - - - - - - -
)

ADD_EXECUTABLE(testB
    !test_files/testB.cpp
    !test_includes/random.cpp
    !test_includes/utilities.cpp
    #- - - - - - - - - - - - - - - - 
    # cpp files we are testing (no commas)

    #- - - - - - - - - - - - - - - - 
)

TARGET_LINK_LIBRARIES(testA gtest)
TARGET_LINK_LIBRARIES(testB gtest)

Full error output:

C:\Users\melhe\Desktop\rational_num\rational_num\build>cmake` -G "MinGW Makefiles" ..
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/melhe/Desktop/rational_num/rational_num/build/googletest
"C:\Program Files\CMake\bin\cmake.exe" -SC:\Users\melhe\Desktop\rational_num\rational_num\build\googletest -BC:\Users\melhe\Desktop\rational_num\rational_num\build\googletest --check-build-system CMakeFiles\Makefile.cmake 0
"C:\Program Files\CMake\bin\cmake.exe" -E cmake_progress_start C:\Users\melhe\Desktop\rational_num\rational_num\build\googletest\CMakeFiles C:\Users\melhe\Desktop\rational_num\rational_num\build\googletest\\CMakeFiles\progress.marks
C:/MinGW/bin/mingw32-make.exe -s -f CMakeFiles\Makefile2 all
[ 11%] Performing update step for 'googletest'
[ 22%] No patch step for 'googletest'
[ 33%] No configure step for 'googletest'
[ 44%] No build step for 'googletest'
[ 55%] No install step for 'googletest'
[ 66%] No test step for 'googletest'
[ 77%] Completed 'googletest'
[100%] Built target googletest
"C:\Program Files\CMake\bin\cmake.exe" -E cmake_progress_start C:\Users\melhe\Desktop\rational_num\rational_num\build\googletest\CMakeFiles 0
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/melhe/Desktop/rational_num/rational_num/build

C:\Users\melhe\Desktop\rational_num\rational_num\build>make
"C:\Program Files\CMake\bin\cmake.exe" -SC:\Users\melhe\Desktop\rational_num\rational_num -BC:\Users\melhe\Desktop\rational_num\rational_num\build --check-build-system CMakeFiles\Makefile.cmake 0
"C:\Program Files\CMake\bin\cmake.exe" -E cmake_progress_start C:\Users\melhe\Desktop\rational_num\rational_num\build\CMakeFiles C:\Users\melhe\Desktop\rational_num\rational_num\build\\CMakeFiles\progress.marks
make -s -f CMakeFiles\Makefile2 all
[ 20%] Built target main
[ 24%] Building CXX object googletest/googletest-build/googlemock/gtest/CMakeFiles/gtest.dir/src/gtest-all.cc.obj
In file included from C:\Users\melhe\Desktop\rational_num\rational_num\build\googletest\googletest-src\googletest\src\gtest-all.cc:42:
C:/Users/melhe/Desktop/rational_num/rational_num/build/googletest/googletest-src/googletest/src/gtest.cc: In function 'testing::internal::TimeInMillis testing::internal::GetTimeInMillis()':
C:/Users/melhe/Desktop/rational_num/rational_num/build/googletest/googletest-src/googletest/src/gtest.cc:842:26: warning: 'int gettimeofday(timeval*, void*)' is deprecated [-Wdeprecated-declarations]
  842 |   gettimeofday(&now, NULL);
      |                          ^
In file included from C:/Users/melhe/Desktop/rational_num/rational_num/build/googletest/googletest-src/googletest/src/gtest.cc:103,
                 from C:\Users\melhe\Desktop\rational_num\rational_num\build\googletest\googletest-src\googletest\src\gtest-all.cc:42:
c:\mingw\include\sys\time.h:106:53: note: declared here
  106 | int __cdecl __MINGW_NOTHROW __POSIX_2008_DEPRECATED gettimeofday
      |                                                     ^~~~~~~~~~~~
In file included from C:\Users\melhe\Desktop\rational_num\rational_num\build\googletest\googletest-src\googletest\src\gtest-all.cc:42:
C:/Users/melhe/Desktop/rational_num/rational_num/build/googletest/googletest-src/googletest/src/gtest.cc:842:26: warning: 'int gettimeofday(timeval*, void*)' is deprecated [-Wdeprecated-declarations]
  842 |   gettimeofday(&now, NULL);
      |                          ^
In file included from C:/Users/melhe/Desktop/rational_num/rational_num/build/googletest/googletest-src/googletest/src/gtest.cc:103,
                 from C:\Users\melhe\Desktop\rational_num\rational_num\build\googletest\googletest-src\googletest\src\gtest-all.cc:42:
c:\mingw\include\sys\time.h:106:53: note: declared here
  106 | int __cdecl __MINGW_NOTHROW __POSIX_2008_DEPRECATED gettimeofday
      |                                                     ^~~~~~~~~~~~
In file included from C:\Users\melhe\Desktop\rational_num\rational_num\build\googletest\googletest-src\googletest\src\gtest-all.cc:45:
C:/Users/melhe/Desktop/rational_num/rational_num/build/googletest/googletest-src/googletest/src/gtest-port.cc: In constructor 'testing::internal::Mutex::Mutex()':
C:/Users/melhe/Desktop/rational_num/rational_num/build/googletest/googletest-src/googletest/src/gtest-port.cc:242:29: error: cannot convert 'CRITICAL_SECTION*' {aka '_CRITICAL_SECTION*'} to '_RTL_CRITICAL_SECTION*' in initialization
  242 |       critical_section_(new CRITICAL_SECTION) {
      |                             ^~~~~~~~~~~~~~~~
      |                             |
      |                             CRITICAL_SECTION* {aka _CRITICAL_SECTION*}
C:/Users/melhe/Desktop/rational_num/rational_num/build/googletest/googletest-src/googletest/src/gtest-port.cc:243:31: error: cannot convert '_RTL_CRITICAL_SECTION*' to 'LPCRITICAL_SECTION' {aka '_CRITICAL_SECTION*'}
  243 |   ::InitializeCriticalSection(critical_section_);
      |                               ^~~~~~~~~~~~~~~~~
      |                               |
      |                               _RTL_CRITICAL_SECTION*
In file included from c:\mingw\include\windows.h:44,
                 from C:/Users/melhe/Desktop/rational_num/rational_num/build/googletest/googletest-src/googletest/src/gtest.cc:108,
                 from C:\Users\melhe\Desktop\rational_num\rational_num\build\googletest\googletest-src\googletest\src\gtest-all.cc:42:
c:\mingw\include\winbase.h:1981:51: note:   initializing argument 1 of 'void InitializeCriticalSection(LPCRITICAL_SECTION)'
 1981 | WINBASEAPI VOID WINAPI InitializeCriticalSection (LPCRITICAL_SECTION);
      |                                                   ^~~~~~~~~~~~~~~~~~
In file included from C:\Users\melhe\Desktop\rational_num\rational_num\build\googletest\googletest-src\googletest\src\gtest-all.cc:45:
C:/Users/melhe/Desktop/rational_num/rational_num/build/googletest/googletest-src/googletest/src/gtest-port.cc: In destructor 'testing::internal::Mutex::~Mutex()':
C:/Users/melhe/Desktop/rational_num/rational_num/build/googletest/googletest-src/googletest/src/gtest-port.cc:253:29: error: cannot convert '_RTL_CRITICAL_SECTION*' to 'PCRITICAL_SECTION' {aka '_CRITICAL_SECTION*'}
  253 |     ::DeleteCriticalSection(critical_section_);
      |                             ^~~~~~~~~~~~~~~~~
      |                             |
      |                             _RTL_CRITICAL_SECTION*
In file included from c:\mingw\include\windows.h:44,
                 from C:/Users/melhe/Desktop/rational_num/rational_num/build/googletest/googletest-src/googletest/src/gtest.cc:108,
                 from C:\Users\melhe\Desktop\rational_num\rational_num\build\googletest\googletest-src\googletest\src\gtest-all.cc:42:
c:\mingw\include\winbase.h:1500:47: note:   initializing argument 1 of 'void DeleteCriticalSection(PCRITICAL_SECTION)'
 1500 | WINBASEAPI void WINAPI DeleteCriticalSection (PCRITICAL_SECTION);
      |                                               ^~~~~~~~~~~~~~~~~
In file included from C:\Users\melhe\Desktop\rational_num\rational_num\build\googletest\googletest-src\googletest\src\gtest-all.cc:45:
C:/Users/melhe/Desktop/rational_num/rational_num/build/googletest/googletest-src/googletest/src/gtest-port.cc: In member function 'void testing::internal::Mutex::Lock()':
C:/Users/melhe/Desktop/rational_num/rational_num/build/googletest/googletest-src/googletest/src/gtest-port.cc:261:26: error: cannot convert '_RTL_CRITICAL_SECTION*' to 'LPCRITICAL_SECTION' {aka '_CRITICAL_SECTION*'}
  261 |   ::EnterCriticalSection(critical_section_);
      |                          ^~~~~~~~~~~~~~~~~
      |                          |
      |                          _RTL_CRITICAL_SECTION*
In file included from c:\mingw\include\windows.h:44,
                 from C:/Users/melhe/Desktop/rational_num/rational_num/build/googletest/googletest-src/googletest/src/gtest.cc:108,
                 from C:\Users\melhe\Desktop\rational_num\rational_num\build\googletest\googletest-src\googletest\src\gtest-all.cc:42:
c:\mingw\include\winbase.h:1531:46: note:   initializing argument 1 of 'void EnterCriticalSection(LPCRITICAL_SECTION)'
 1531 | WINBASEAPI void WINAPI EnterCriticalSection (LPCRITICAL_SECTION);
      |                                              ^~~~~~~~~~~~~~~~~~
In file included from C:\Users\melhe\Desktop\rational_num\rational_num\build\googletest\googletest-src\googletest\src\gtest-all.cc:45:
C:/Users/melhe/Desktop/rational_num/rational_num/build/googletest/googletest-src/googletest/src/gtest-port.cc: In member function 'void testing::internal::Mutex::Unlock()':
C:/Users/melhe/Desktop/rational_num/rational_num/build/googletest/googletest-src/googletest/src/gtest-port.cc:271:26: error: cannot convert '_RTL_CRITICAL_SECTION*' to 'LPCRITICAL_SECTION' {aka '_CRITICAL_SECTION*'}
  271 |   ::LeaveCriticalSection(critical_section_);
      |                          ^~~~~~~~~~~~~~~~~
      |                          |
      |                          _RTL_CRITICAL_SECTION*
In file included from c:\mingw\include\windows.h:44,
                 from C:/Users/melhe/Desktop/rational_num/rational_num/build/googletest/googletest-src/googletest/src/gtest.cc:108,
                 from C:\Users\melhe\Desktop\rational_num\rational_num\build\googletest\googletest-src\googletest\src\gtest-all.cc:42:
c:\mingw\include\winbase.h:2043:46: note:   initializing argument 1 of 'void LeaveCriticalSection(LPCRITICAL_SECTION)'
 2043 | WINBASEAPI void WINAPI LeaveCriticalSection (LPCRITICAL_SECTION);
      |                                              ^~~~~~~~~~~~~~~~~~
In file included from C:\Users\melhe\Desktop\rational_num\rational_num\build\googletest\googletest-src\googletest\src\gtest-all.cc:45:
C:/Users/melhe/Desktop/rational_num/rational_num/build/googletest/googletest-src/googletest/src/gtest-port.cc: In member function 'void testing::internal::Mutex::ThreadSafeLazyInit()':
C:/Users/melhe/Desktop/rational_num/rational_num/build/googletest/googletest-src/googletest/src/gtest-port.cc:292:29: error: cannot convert 'CRITICAL_SECTION*' {aka '_CRITICAL_SECTION*'} to '_RTL_CRITICAL_SECTION*' in assignment
  292 |         critical_section_ = new CRITICAL_SECTION;
      |                             ^~~~~~~~~~~~~~~~~~~~
      |                             |
      |                             CRITICAL_SECTION* {aka _CRITICAL_SECTION*}
C:/Users/melhe/Desktop/rational_num/rational_num/build/googletest/googletest-src/googletest/src/gtest-port.cc:293:37: error: cannot convert '_RTL_CRITICAL_SECTION*' to 'LPCRITICAL_SECTION' {aka '_CRITICAL_SECTION*'}
  293 |         ::InitializeCriticalSection(critical_section_);
      |                                     ^~~~~~~~~~~~~~~~~
      |                                     |
      |                                     _RTL_CRITICAL_SECTION*
In file included from c:\mingw\include\windows.h:44,
                 from C:/Users/melhe/Desktop/rational_num/rational_num/build/googletest/googletest-src/googletest/src/gtest.cc:108,
                 from C:\Users\melhe\Desktop\rational_num\rational_num\build\googletest\googletest-src\googletest\src\gtest-all.cc:42:
c:\mingw\include\winbase.h:1981:51: note:   initializing argument 1 of 'void InitializeCriticalSection(LPCRITICAL_SECTION)'
 1981 | WINBASEAPI VOID WINAPI InitializeCriticalSection (LPCRITICAL_SECTION);
      |                                                   ^~~~~~~~~~~~~~~~~~
googletest\googletest-build\googlemock\gtest\CMakeFiles\gtest.dir\build.make:82: recipe for target 'googletest/googletest-build/googlemock/gtest/CMakeFiles/gtest.dir/src/gtest-all.cc.obj' failed
make[2]: *** [googletest/googletest-build/googlemock/gtest/CMakeFiles/gtest.dir/src/gtest-all.cc.obj] Error 1
CMakeFiles\Makefile2:295: recipe for target 'googletest/googletest-build/googlemock/gtest/CMakeFiles/gtest.dir/all' failed
make[1]: *** [googletest/googletest-build/googlemock/gtest/CMakeFiles/gtest.dir/all] Error 2
Makefile:148: recipe for target 'all' failed
make: *** [all] Error 2
@ghost
Copy link

ghost commented Jan 14, 2021

Hello

I think it is also possible to use Cygwin unless you have to use a specific tool
I personally use Cygwin on Windows and it works well as below

# cygwin installed with build packages (gcc-g++, make, cmake, git, vim, ..)
D:\> git clone -b 'release-1.10.0' https://github.com/google/googletest.git
D:\> cd googletest && mkdir build && cd build && cmake .. && make && cd ../../
D:\> cat gtest_main.cpp

#include "gtest/gtest.h"
TEST(Assert, Eq) {  ASSERT_EQ(1, 1);  }
int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

D:\> g++ -std=gnu++11 -o gtest gtest_main.cpp -Igoogletest/googletest/include -pthread -Lgoogletest/build/lib -lgtest
D:\> gtest.exe

[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from Assert
[ RUN      ] Assert.Eq
[       OK ] Assert.Eq (1 ms)
[----------] 1 test from Assert (1 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (27 ms total)
[  PASSED  ] 1 test.

@melh23
Copy link
Author

melh23 commented Jan 22, 2021

Thanks for your help! This project setup helped. I was actually able to get it running without Cygwin. I needed to install Windows 10 SDK through the Visual Studio installer, but it worked.

@melh23 melh23 closed this as completed Jan 22, 2021
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

No branches or pull requests

1 participant