GMock doesn't compile - GTEST_EXCLUSIVE_LOCK_REQUIRED seems to not be defined
-
23-12-2019 - |
Question
I'm trying to build a simple mocked class
#include "interpolation.hpp"
#include <gtest/gtest.h>
#include <gmock/gmock.h>
class MockInterp1D : public Interp1DBase {
public:
MOCK_METHOD1(evaluateAt, double(double));
MOCK_METHOD2(evaluateAt, double(double, int));
};
based on the following base class
class Interp1DBase {
public:
virtual double evaluateAt(double) const = 0;
virtual double evaluateAt(double, int) const = 0;
virtual ~Interp1DBase() { };
};
using Google Mocks. When I try to compile the tests where this mock is used, I get the following error:
In file included from /usr/include/gmock/gmock-generated-function-mockers.h:43:0,
from /usr/include/gmock/gmock.h:61,
from /home/tlycken/exjobb/Code/alpha-orbit-follower/test/interpolation/interpolation-mocks.hpp:4,
from /home/tlycken/exjobb/Code/alpha-orbit-follower/test/physics/B-field-tests.hpp:6,
from /home/tlycken/exjobb/Code/alpha-orbit-follower/test/physics/B-field-tests.cpp:2:
/usr/include/gmock/gmock-spec-builders.h:134:41: error: expected ‘;’ at end of member declaration
bool VerifyAndClearExpectationsLocked()
^
and then literally hundreds of similar syntax or definition errors all pointing to files within GMock.
I took a look at gmock-spec-builder.h:134
, and found the following (in some context):
// Verifies that all expectations on this mock function have been
// satisfied. Reports one or more Google Test non-fatal failures
// and returns false if not.
bool VerifyAndClearExpectationsLocked()
GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex);
which led me to believe that GTEST_EXCLUSIVE_LOCK_REQUIRED_
might be a macro that for some reason wasn't defined. And indeed, after digging through all header files included from either gmock/gmock.h
or gtest/gtest.h
I still haven't found the definition of that macro.
What am I doing wrong here?
UPDATE:
I've been able to produce an even simple minimal example:
// in file mock-test.cpp
#include <gmock/gmock.h>
// Yeah, that's the only content
Compile with
g++ -o mock-test.o -c mock-test.cpp
Causes the same error.
I've installed GMock through sudo apt-get install google-mock
, which gave me a folder under /usr/src
where I could run cmake .
followed by make
to generate library files which I copied to /usr/lib
. The header files were already in /usr/include
so I didn't do anything about them manually.
Solution
I got the same error when I tried to compile using gmock 1.7 with gtest 1.6. Make sure you are using the same version of gtest.
OTHER TIPS
I don't know how to use g-mock in gcc
.
But basically we use in windows like this.
Your class definitions are right.
Did you use those functions like
EXPECT_CALL(classobj, exact functionname as it looks in definition).AtLeast(Times(0)).Return(0);
then
classobj.exact functionname as it looks in definition()
Try this I think it should work. If you want it in detail, just let me know I will expand the same with some typical example.
There are the appropriate sources & headers in the /usr/src/gmock folder.
Your job is only overwrite the whole content of folders:
/usr/src/gmock/gtest/src -> /usr/src/gtest/src /usr/src/gmock/gtest/cmake -> /usr/src/gtest/cmake /usr/src/gmock/gtest/CMakeLists.txt-> /usr/src/gtest/CMakeLists.txt /usr/src/gmock/gtest/include/gtest -> /usr/include/gtest/