google / googletest

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

Compilation error, ‘SleepMilliseconds’ was not declared in this scope #3468

Open Mr-Leshiy opened 3 years ago

Mr-Leshiy commented 3 years ago

Describe the bug

I have got a compilation error

[ 24%] Linking CXX executable gtest_throw_on_failure_ex_test
/home/leshiy/Project/googletest/googletest/test/googletest-port-test.cc: In member function ‘virtual void testing::internal::GetThreadCountTest_ReturnsCorrectValue_Test::TestBody()’:
/home/leshiy/Project/googletest/googletest/test/googletest-port-test.cc:318:5: error: ‘SleepMilliseconds’ was not declared in this scope
  318 |     SleepMilliseconds(100);
      |     ^~~~~~~~~~~~~~~~~
[ 24%] Built target gtest_throw_on_failure_ex_test
[ 24%] Building CXX object googletest/CMakeFiles/gtest_skip_test.dir/test/gtest_skip_test.cc.o
[ 24%] Linking CXX executable gtest-death-test_ex_nocatch_test

Steps to reproduce the bug

Build project with the following lines

mkdir build
cd build
cmake .. -Dgtest_build_tests=ON -Dgtest_build_samples=ON -Dgtest_disable_pthreads=ON -Dgmock_build_tests=ON
make

What build system are you using?

[leshiy@leshiy-g33579 googletest]$ cmake -version
cmake version 3.20.3

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

Hi @Mr-Leshiy, I searched for that function in GoogleTest's source code and found it in googletest/include/gtest/internal/gtest-port.h:

// Defines synchronization primitives.
#if GTEST_IS_THREADSAFE
# if GTEST_HAS_PTHREAD
// Sleeps for (roughly) n milliseconds.  This function is only for testing
// Google Test's own constructs.  Don't use it in user tests, either
// directly or indirectly.
inline void SleepMilliseconds(int n) {
  const timespec time = {
    0,                  // 0 seconds.
    n * 1000L * 1000L,  // And n ms.
  };
  nanosleep(&time, nullptr);
}
# endif  // GTEST_HAS_PTHREAD

It looks like you're trying to use a GTEST-internal function within your test suite. If you just want to make gtest wait, maybe this thread from stackoverflow can help.

Mr-Leshiy commented 3 years ago

@Schallbert , I am just trying to build the original googletest project, it is not about that I have problems with the usage of the gtest in my own projects.

Schallbert commented 3 years ago

@Mr-Leshiy, thanks for the clarification. To further track things down: did you install the pthtead library? When I tried to build with MinGW in the past and didn't have this package installed, it would fail for a similar reason. Refer to this post on my website to see the prerequisites for googletest when it comes to installed libraries.

ghost commented 3 years ago

Hello, I'm not sure if it will be accepted But, this https://github.com/google/googletest/pull/3482 seems to solve the above issue

derekmauro commented 3 years ago

Can I ask why are you building this with -Dgtest_disable_pthreads=ON?

Mr-Leshiy commented 3 years ago

@derekmauro , I am was trying to build gtest with all tests and was trying different combinations of build. It is not something that I use in the project.