Qt 中的 GMock 和未定义引用错误

GMock and Undefined References Error in Qt

我正在尝试 google 在 Windows 中使用 Qt Creator 进行模拟测试,但结果出现了很多不同的 undefined reference 错误。

我在 Linux 中尝试过,它正在运行。 Windows.

不知道是不是我设置错了 gmock

这是在 MOCK_CONST_METHOD2MOCK_METHOD2MOCK_METHOD1 和前两个 EXPECT_CALL 上存在问题的代码。 gmock header 文件也有问题。

main.cpp

#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "mail_service.hpp"
#include "warehouse.hpp"
#include "order.hpp"

using ::testing::Return;
using ::testing::_; // Matcher for parameters

/** \brief Mock for the warehouse interface.
 * \author David Stutz
 */
class MockWarehouse : public Warehouse
{
public:

    // see https://github.com/google/googletest/blob/master/googlemock/docs/ForDummies.md
    MOCK_CONST_METHOD2(hasInventory, bool(int, std::string));
    MOCK_METHOD2(remove, void(int, std::string));
};

/** \brief Mock for the mail service interface.
 * \author David Stutz
 */
class MockMailService : public MailService
{
public:
    MockMailService()
    {

    }

    MOCK_METHOD1(send, void(std::string));
};

TEST(OrderTest, Fill)
{
    MockWarehouse warehouse;
    std::shared_ptr<MockMailService> mailService = std::make_shared<MockMailService>();

    Order order(50, "Talisker");
    order.setMailService(mailService);

    EXPECT_CALL(warehouse, hasInventory(50, "Talisker"))
        .Times(1)
        .WillOnce(Return(true));
    EXPECT_CALL(warehouse, remove(50, "Talisker"))
        .Times(1);

    EXPECT_CALL(*mailService, send(_)) // Not making assumptions on the message send ...
        .Times(1);

    ASSERT_TRUE(order.fill(warehouse));
}

/** \brief Main test entrance point. 
 * \param[in] argc
 * \param[in] argv
 */
int main(int argc, char** argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

headers
mail_service.hpp

#ifndef MAIL_SERVICE_HPP
#define MAIL_SERVICE_HPP


class MailService
{
public:
    /** \brief Send a mial.
     * \param[in] message message to send
     */
    virtual void send(std::string message) = 0;

};

#endif /* MAIL_SERVICE_HPP */

order.hpp

#ifndef ORDER_HPP
#define ORDER_HPP

#include <string>
#include <memory>
#include "warehouse.hpp"
#include "mail_service.hpp"

/** \brief An order of a product with quantity. */
class Order
{
public:
    /** \brief Constructor.
     * \param[in] quantity quantity requested
     * \param[in] product product name requested
     */
    Order(int quantity, std::string product)
    {
        this->quantity = quantity;
        this->product = product;
    }

    /** \brief Set the mail service to use. 
     * \param[in] mailService the mail service to attach
     */
    void setMailService(std::shared_ptr<MailService> mailService)
    {
        this->mailService = mailService;
    }

    /** \brief Fill the order given the warehouse. 
     * \param[in] warehouse the warehouse to use
     * \return whether the operation was successful
     */
    bool fill(Warehouse &warehouse)
    {
        if (warehouse.hasInventory(quantity, product))
        {
            // ...
            warehouse.remove(quantity, product);
            this->mailService->send("Order filled.");

            return true;
        }
        else
        {
            // ...
            this->mailService->send("Order not filled.");

            return false;
        }
    }

private:

    /** \brief Product name. */
    std::string product;

    /** \brief Quantity requested. */
    int quantity;

    /** \brief Mail service to use. */
    std::shared_ptr<MailService> mailService;
};

#endif /* ORDER_HPP */

warehouse.hpp

#ifndef WAREHOUSE_HPP
#define WAREHOUSE_HPP

#include <string>

/** \brief Warehouse interface. This interface is one of the collaborators of our SUT.
class Warehouse
{
public:
    /** \brief Check whether the product in the given quantity is on stock.
     * \param[in] quantity quantity requested
     * \param[in] product product name
     * \return whether the warehouse has the product on stock for the given quantity
     */
    virtual bool hasInventory(int quantity, std::string product) const = 0;

    /** \brief Remove the given quantity of the product from the warehouse.
     * \param[in] quantity quantity to remove
     * \param[in] product product name to remove
     */
    virtual void remove(int quantity, std::string product) = 0;

};

#endif /* WAREHOUSE_HPP */

这是makefile

这是 .pro 文件

CONFIG += console c++14

INCLUDEPATH += "google/gmock/include/"
INCLUDEPATH += "google/gmock/"
INCLUDEPATH += "google/gtest/include/"
INCLUDEPATH += "google/gtest/src/"
INCLUDEPATH += "google/gtest/"
INCLUDEPATH += "google/"
INCLUDEPATH += "../../"

SOURCES += \
    order.cpp \
    google/gtest/src/gtest.cc \
    google/gtest/src/gtest-all.cc \
    google/gtest/src/gtest-death-test.cc \
    google/gtest/src/gtest-filepath.cc \
    google/gtest/src/gtest-port.cc \
    google/gtest/src/gtest-printers.cc \
    google/gtest/src/gtest-test-part.cc \
    google/gtest/src/gtest-typed-test.cc

HEADERS += \
    mail_service.hpp \
    order.hpp \
    warehouse.hpp

这里是编译输出错误:

10:53:25: Running steps for project GoogleMockTest2...
10:53:25: Configuration unchanged, skipping qmake step.
10:53:27: Starting: "C:\Qt\Qt5.9.3\Tools\mingw530_32\bin\mingw32-make.exe" 
C:\Qt\Qt5.9.3.9.3\mingw53_32\bin\qmake.exe -o Makefile ..\GoogleMockTest2\GoogleMockTest2.pro -spec win32-g++ "CONFIG+=debug" "CONFIG+=qml_debug"
C:/Qt/Qt5.9.3/Tools/mingw530_32/bin/mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory 'C:/Users/PB9N0052/Documents/build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug'
g++ -c -fno-keep-inline-dllexport -g -std=gnu++1y -Wextra -Wall -W -fexceptions -mthreads -DUNICODE -D_UNICODE -DQT_QML_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I..\GoogleMockTest2 -I. -I..\GoogleMockTest2\google\googlemock\include -I..\GoogleMockTest2\google\googlemock -I..\GoogleMockTest2\google\googletest\include -I..\GoogleMockTest2\google\googletest\src -I..\GoogleMockTest2\google\googletest -I..\GoogleMockTest2\google -I..\..\..\PB9N0052 -I..\..\..\..\Qt\Qt5.9.3.9.3\mingw53_32\include -I..\..\..\..\Qt\Qt5.9.3.9.3\mingw53_32\include\QtGui -I..\..\..\..\Qt\Qt5.9.3.9.3\mingw53_32\include\QtANGLE -I..\..\..\..\Qt\Qt5.9.3.9.3\mingw53_32\include\QtCore -Idebug -I..\..\..\..\Qt\Qt5.9.3.9.3\mingw53_32\mkspecs\win32-g++  -o debug\warehouse.o ..\GoogleMockTest2\warehouse.cpp
g++ -c -fno-keep-inline-dllexport -g -std=gnu++1y -Wextra -Wall -W -fexceptions -mthreads -DUNICODE -D_UNICODE -DQT_QML_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I..\GoogleMockTest2 -I. -I..\GoogleMockTest2\google\googlemock\include -I..\GoogleMockTest2\google\googlemock -I..\GoogleMockTest2\google\googletest\include -I..\GoogleMockTest2\google\googletest\src -I..\GoogleMockTest2\google\googletest -I..\GoogleMockTest2\google -I..\..\..\PB9N0052 -I..\..\..\..\Qt\Qt5.9.3.9.3\mingw53_32\include -I..\..\..\..\Qt\Qt5.9.3.9.3\mingw53_32\include\QtGui -I..\..\..\..\Qt\Qt5.9.3.9.3\mingw53_32\include\QtANGLE -I..\..\..\..\Qt\Qt5.9.3.9.3\mingw53_32\include\QtCore -Idebug -I..\..\..\..\Qt\Qt5.9.3.9.3\mingw53_32\mkspecs\win32-g++  -o debug\mail_service.o ..\GoogleMockTest2\mail_service.cpp
g++ -Wl,-subsystem,console -mthreads -o debug\GoogleMockTest2.exe debug/order.o debug/gtest-all.o debug/warehouse.o debug/mail_service.o  -LC:\Qt\Qt5.9.3.9.3\mingw53_32\lib C:\Qt\Qt5.9.3.9.3\mingw53_32\lib\libQt5Guid.a C:\Qt\Qt5.9.3.9.3\mingw53_32\lib\libQt5Cored.a 
debug/order.o: In function `ZN19OrderTest_Fill_Test8TestBodyEv':
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/order.cpp:44: undefined reference to `testing::Matcher<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::Matcher(char const*)'
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1416: undefined reference to `testing::Mock::UnregisterLocked(testing::internal::UntypedFunctionMockerBase*)'
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1418: undefined reference to `testing::internal::UntypedFunctionMockerBase::~UntypedFunctionMockerBase()'
debug/order.o: In function `ZN7testing8internal18FunctionMockerBaseIFvNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEED1Ev':
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1414: undefined reference to `testing::internal::g_gmock_mutex'
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1415: undefined reference to `testing::internal::UntypedFunctionMockerBase::VerifyAndClearExpectationsLocked()'
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1416: undefined reference to `testing::Mock::UnregisterLocked(testing::internal::UntypedFunctionMockerBase*)'
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1418: undefined reference to `testing::internal::UntypedFunctionMockerBase::~UntypedFunctionMockerBase()'
debug/order.o: In function `ZN7testing8internal18FunctionMockerBaseIFbiNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEC2Ev':
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1407: undefined reference to `testing::internal::UntypedFunctionMockerBase::UntypedFunctionMockerBase()'
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1407: undefined reference to `testing::internal::UntypedFunctionMockerBase::~UntypedFunctionMockerBase()'
debug/order.o: In function `ZN7testing8internal18FunctionMockerBaseIFbiNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEED2Ev':
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1414: undefined reference to `testing::internal::g_gmock_mutex'
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1415: undefined reference to `testing::internal::UntypedFunctionMockerBase::VerifyAndClearExpectationsLocked()'
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1416: undefined reference to `testing::Mock::UnregisterLocked(testing::internal::UntypedFunctionMockerBase*)'
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1418: undefined reference to `testing::internal::UntypedFunctionMockerBase::~UntypedFunctionMockerBase()'
debug/order.o: In function `ZN7testing8internal18FunctionMockerBaseIFbiNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEED1Ev':
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1414: undefined reference to `testing::internal::g_gmock_mutex'
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1415: undefined reference to `testing::internal::UntypedFunctionMockerBase::VerifyAndClearExpectationsLocked()'
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1416: undefined reference to `testing::Mock::UnregisterLocked(testing::internal::UntypedFunctionMockerBase*)'
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1418: undefined reference to `testing::internal::UntypedFunctionMockerBase::~UntypedFunctionMockerBase()'
debug/order.o: In function `ZN7testing8internal18FunctionMockerBaseIFviNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEC2Ev':
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1407: undefined reference to `testing::internal::UntypedFunctionMockerBase::UntypedFunctionMockerBase()'
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1407: undefined reference to `testing::internal::UntypedFunctionMockerBase::~UntypedFunctionMockerBase()'
debug/order.o: In function `ZN7testing8internal18FunctionMockerBaseIFviNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEED2Ev':
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1414: undefined reference to `testing::internal::g_gmock_mutex'
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1415: undefined reference to `testing::internal::UntypedFunctionMockerBase::VerifyAndClearExpectationsLocked()'
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1416: undefined reference to `testing::Mock::UnregisterLocked(testing::internal::UntypedFunctionMockerBase*)'
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1418: undefined reference to `testing::internal::UntypedFunctionMockerBase::~UntypedFunctionMockerBase()'
debug/order.o: In function `ZN7testing8internal18FunctionMockerBaseIFviNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEED1Ev':
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1414: undefined reference to `testing::internal::g_gmock_mutex'
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1415: undefined reference to `testing::internal::UntypedFunctionMockerBase::VerifyAndClearExpectationsLocked()'
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1416: undefined reference to `testing::Mock::UnregisterLocked(testing::internal::UntypedFunctionMockerBase*)'
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1418: undefined reference to `testing::internal::UntypedFunctionMockerBase::~UntypedFunctionMockerBase()'
debug/order.o: In function `ZN7testing8internal8MockSpecIFbiNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEE18InternalExpectedAtEPKciSB_SB_':
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1271: undefined reference to `testing::internal::LogWithLocation(testing::internal::LogSeverity, char const*, int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
debug/order.o: In function `ZN7testing8internal16TypedExpectationIFbiNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEE5TimesEi':
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:932: undefined reference to `testing::Exactly(int)'
debug/order.o: In function `ZN7testing8internal16TypedExpectationIFbiNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEE8WillOnceERKNS_6ActionIS8_EE':
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1002: undefined reference to `testing::Exactly(int)'
debug/order.o: In function `ZN7testing8internal8MockSpecIFviNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEE18InternalExpectedAtEPKciSB_SB_':
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1271: undefined reference to `testing::internal::LogWithLocation(testing::internal::LogSeverity, char const*, int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
debug/order.o: In function `ZN7testing8internal16TypedExpectationIFviNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEE5TimesEi':
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:932: undefined reference to `testing::Exactly(int)'
debug/order.o: In function `ZN7testing8internal8MockSpecIFvNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEE18InternalExpectedAtEPKciSB_SB_':
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1271: undefined reference to `testing::internal::LogWithLocation(testing::internal::LogSeverity, char const*, int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
debug/order.o: In function `ZN7testing8internal16TypedExpectationIFvNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEE5TimesEi':
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:932: undefined reference to `testing::Exactly(int)'
debug/order.o: In function `ZN7testing8internal18FunctionMockerBaseIFbiNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEE10InvokeWithERKSt5tupleIJiS7_EE':
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1529: undefined reference to `testing::internal::UntypedFunctionMockerBase::UntypedInvokeWith(void const*)'
debug/order.o: In function `ZN7testing8internal18FunctionMockerBaseIFviNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEE10InvokeWithERKSt5tupleIJiS7_EE':
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1529: undefined reference to `testing::internal::UntypedFunctionMockerBase::UntypedInvokeWith(void const*)'
debug/order.o: In function `ZN7testing8internal18FunctionMockerBaseIFvNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEE25ClearDefaultActionsLockedEv':
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1560: undefined reference to `testing::Expectation::~Expectation()'
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1551: undefined reference to `testing::internal::UntypedFunctionMockerBase::MockObject() const'
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1551: undefined reference to `testing::Mock::RegisterUseByOnCallOrExpectCall(void const*, char const*, int)'
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1558: undefined reference to `testing::internal::g_gmock_implicit_sequence'
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1560: undefined reference to `testing::Expectation::Expectation(testing::internal::linked_ptr<testing::internal::ExpectationBase> const&)'
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1560: undefined reference to `testing::Sequence::AddExpectation(testing::Expectation const&) const'
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1560: undefined reference to `testing::Expectation::~Expectation()'
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1560: undefined reference to `testing::Expectation::~Expectation()'
debug/order.o: In function `ZN7testing8internal16TypedExpectationIFvNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEE5TimesERKNS_11CardinalityE':
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:926: undefined reference to `testing::internal::ExpectationBase::UntypedTimes(testing::Cardinality const&)'
debug/order.o: In function `ZN7testing8internal16TypedExpectationIFbiNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEC1EPNS0_18FunctionMockerBaseIS8_EEPKciRKS7_RKSt5tupleIJNS_7MatcherIiEENSI_IS7_EEEE':
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:894: undefined reference to `testing::internal::ExpectationBase::ExpectationBase(char const*, int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:894: undefined reference to `testing::internal::ExpectationBase::~ExpectationBase()'
debug/order.o: In function `ZN7testing8internal16TypedExpectationIFviNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEC1EPNS0_18FunctionMockerBaseIS8_EEPKciRKS7_RKSt5tupleIJNS_7MatcherIiEENSI_IS7_EEEE':
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:899: undefined reference to `testing::internal::ExpectationBase::CheckActionCountIfNotDone() const'
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:904: undefined reference to `testing::internal::ExpectationBase::~ExpectationBase()'
debug/order.o: In function `ZN7testing8internal16TypedExpectationIFbiNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEE9GetHandleEv':
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1077: undefined reference to `testing::internal::UntypedFunctionMockerBase::GetHandleOf(testing::internal::ExpectationBase*)'
debug/order.o: In function `ZNK7testing8internal18FunctionMockerBaseIFvNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEE32UntypedDescribeUninterestingCallEPKvPSo':
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1603: undefined reference to `testing::internal::UntypedFunctionMockerBase::Name() const'
debug/order.o: In function `ZN7testing8internal18FunctionMockerBaseIFvNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEE30UntypedFindMatchingExpectationEPKvPSB_PbPSoSE_':
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1630: undefined reference to `testing::internal::g_gmock_mutex'
debug/order.o: In function `ZNK7testing8internal18FunctionMockerBaseIFviNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEE32UntypedDescribeUninterestingCallEPKvPSo':
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1603: undefined reference to `testing::internal::UntypedFunctionMockerBase::Name() const'
debug/order.o: In function `ZN7testing8internal18FunctionMockerBaseIFviNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEE30UntypedFindMatchingExpectationEPKvPSB_PbPSoSE_':
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1630: undefined reference to `testing::internal::g_gmock_mutex'
debug/order.o: In function `ZNK7testing8internal18FunctionMockerBaseIFbiNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEE32UntypedDescribeUninterestingCallEPKvPSo':
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1603: undefined reference to `testing::internal::UntypedFunctionMockerBase::Name() const'
debug/order.o: In function `ZN7testing8internal18FunctionMockerBaseIFbiNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEE30UntypedFindMatchingExpectationEPKvPSB_PbPSoSE_':
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1630: undefined reference to `testing::internal::g_gmock_mutex'
debug/order.o: In function `ZNK7testing8internal18FunctionMockerBaseIFvNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEE29FindMatchingExpectationLockedERKSt5tupleIJS7_EE':
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1661: undefined reference to `testing::internal::g_gmock_mutex'
debug/order.o: In function `ZNK7testing8internal18FunctionMockerBaseIFvNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEE33FormatUnexpectedCallMessageLockedERKSt5tupleIJS7_EEPSoSE_':
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1110: undefined reference to `testing::internal::g_gmock_mutex'
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1128: undefined reference to `testing::internal::ExpectationBase::AllPrerequisitesAreSatisfied() const'
C:\Users\PB9N0052\Documents\build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug/../GoogleMockTest2/google/gmock/include/gmock/gmock-spec-builders.h:1133: undefined reference to `testing::internal::ExpectationBase::FindUnsatisfiedPrerequisites(testing::ExpectationSet*) const'
collect2.exe: error: ld returned 1 exit status
mingw32-make[1]: *** [debug\GoogleMockTest2.exe] Error 1
mingw32-make: *** [debug] Error 2
Makefile.Debug:74: recipe for target 'debug\GoogleMockTest2.exe' failed
mingw32-make[1]: Leaving directory 'C:/Users/PB9N0052/Documents/build-GoogleMockTest2-Desktop_Qt_5_9_3_MinGW_32bit-Debug'
Makefile:36: recipe for target 'debug' failed
10:53:32: The process "C:\Qt\Qt5.9.3\Tools\mingw530_32\bin\mingw32-make.exe" exited with code 2.
Error while building/deploying project GoogleMockTest2 (kit: Desktop Qt 5.9.3 MinGW 32bit)
When executing step "Make"
10:53:32: Elapsed time: 00:07.

也许您忘记在源代码中添加 gmock 的“.cc”文件。 因为在你的.pro文件中,你只添加了gtest的“.cc”文件。

我经常做的是,"add existing files" 然后寻找 gmock 的 src 文件夹。 并添加 .cc 文件。

喜欢这个样本:

SOURCES += \
    ../calculator.cpp \
    ../button.cpp \
    main.cpp \
    gtest/src/gtest.cc \
    gtest/src/gtest-all.cc \
    gtest/src/gtest-death-test.cc \
    gtest/src/gtest-filepath.cc \
    gtest/src/gtest-port.cc \
    gtest/src/gtest-printers.cc \
    gtest/src/gtest-test-part.cc \
    gtest/src/gtest-typed-test.cc \
    gmock/src/gmock-spec-builders.cc \
    gmock/src/gmock-matchers.cc \
    gmock/src/gmock-internal-utils.cc \
    gmock/src/gmock-cardinalities.cc \
    gmock/src/gmock-all.cc \
    gmock/src/gmock.cc

我的新 .pro 文件

CONFIG += console c++14

INCLUDEPATH += "google/googlemock/include/"
INCLUDEPATH += "google/googlemock/"
INCLUDEPATH += "google/googletest/include/"
INCLUDEPATH += "google/googletest/src/"
INCLUDEPATH += "google/googletest/"
INCLUDEPATH += "google/"
INCLUDEPATH += "../../"

SOURCES += \
    order.cpp \
    google/googletest/src/gtest.cc \
    google/googletest/src/gtest-all.cc \
    google/googletest/src/gtest-death-test.cc \
    google/googletest/src/gtest-filepath.cc \
    google/googletest/src/gtest-port.cc \
    google/googletest/src/gtest-printers.cc \
    google/googletest/src/gtest-test-part.cc \
    google/googletest/src/gtest-typed-test.cc \
    google/googlemock/src/gmock.cc \
    google/googlemock/src/gmock-all.cc \
    google/googlemock/src/gmock-cardinalities.cc \
    google/googlemock/src/gmock-internal-utils.cc \
    google/googlemock/src/gmock-matchers.cc \
    google/googlemock/src/gmock-spec-builders.cc

HEADERS += \
    mail_service.hpp \
    order.hpp \
    warehouse.hpp

我遇到了同样的问题,包含 gmock 和 gtest 源文件的指针是正确的引导,但我仍然遇到链接错误(我使用的是 CMake)。

错误是因为函数的多重定义。问题是 gtest-all.cc 包括 gtest 的所有相关来源(并且 gmock-all.cc 对 gmock 来源做同样的事情)。所以你需要做的就是包括 gtest-all.cc 和 gmock-all.cc:

SOURCES += \
    ../calculator.cpp \
    ../button.cpp \
    main.cpp \
    gtest/src/gtest-all.cc \
    gmock/src/gmock-all.cc

这样也干净多了。

我希望这是对上述答案的一个很好的补充解决方案,因为它解决了下一个问题(至少对我而言)。