GMock 不编译 - GTEST_EXCLUSIVE_LOCK_REQUIRED似乎没有定义

GMock doesn't compile - GTEST_EXCLUSIVE_LOCK_REQUIRED seems to not be defined

本文关键字:定义 REQUIRED EXCLUSIVE 编译 GTEST GMock LOCK      更新时间:2023-10-16

我正在尝试构建一个简单的模拟类

#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));
};

基于以下基本类

class Interp1DBase {
public:
virtual double evaluateAt(double) const = 0;
virtual double evaluateAt(double, int) const = 0;
virtual ~Interp1DBase() { };
};

使用Google Mocks。当我试图编译使用该mock的测试时,我得到了以下错误:

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()
^

然后是数百个类似的语法或定义错误,所有错误都指向GMock中的文件。

我查看了gmock-spec-builder.h:134,发现了以下内容(在某些上下文中):

// 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);

这让我相信CCD_ 2可能是一个由于某种原因没有定义的宏。事实上,在挖掘了gmock/gmock.hgtest/gtest.h中包含的所有头文件之后,我仍然没有找到该宏的定义。

我在这里做错了什么?


更新:

我已经能够生成一个甚至简单的最小示例:

// in file mock-test.cpp
#include <gmock/gmock.h>
// Yeah, that's the only content

使用编译

g++ -o mock-test.o -c mock-test.cpp

导致相同的错误。

我通过sudo apt-get install google-mock安装了GMock,它在/usr/src下给了我一个文件夹,在那里我可以运行cmake .,然后运行make来生成库文件,并将其复制到/usr/lib。头文件已经在/usr/include中了,所以我没有手动对它们做任何操作。

当我尝试使用gmock 1.7和gtest 1.6进行编译时,我也遇到了同样的错误。请确保您使用的是相同版本的gtest。

我不知道如何在gcc中使用g-mock。但基本上我们在这样的窗口中使用。你的类定义是正确的。你用过这样的功能吗

EXPECT_CALL(classobj, exact functionname as it looks in definition).AtLeast(Times(0)).Return(0);

然后

classobj.exact functionname as it looks in definition()

试试这个,我觉得它应该有效。如果你想详细了解它,请告诉我,我会用一些典型的例子来扩展它。

有适当的来源&/usr/src/gmock文件夹中的标头。

你的工作只是覆盖文件夹的全部内容:

/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/