c++ - 对构造函数和析构函数的 undefined reference

标签 c++ unit-testing googletest googlemock

我正在尝试使用 google testgoogle mock 编写单元测试,但我对此没有什么困难。我遇到错误(您可以在下面看到)我无法修复。

DummyUT.o: In function `~MockApplicationClass':
srctest/../mocks/AppMock.h:15: undefined reference to `Application::~Application()'
srctest/../mocks/AppMock.h:15: undefined reference to `Application::~Application()'
DummyUT.o: In function `MockApplicationClass':
srctest/../mocks/AppMock.h:15: undefined reference to `Application::Application()'
srctest/../mocks/AppMock.h:15: undefined reference to `Application::~Application()'
collect2: ld returned 1 exit status
make: *** [TestApp] Error 1

这是我的 AppMock.h 文件:

#ifndef APPMOCK_H_
#define APPMOCK_H_
#include "../../app.hxx"
#include "gmock/gmock.h"
class MockApplicationClass: public Application
{
  public:
    MOCK_CONST_METHOD2(Reset, void(int i, const char* chr));
};
#endif /* APPMOCK_H_ */

这里是app.hxx文件:

class Application {
   public:
      Application();
      ~Application();
      int Reset(int i, const char* chr)
}

构造函数和析构函数在文件 app.cxx 中定义。析构函数是微不足道的:

//Deconstruction method
Application::~Application() {
    // release all resources before exit

} // end of ~Application()

DummyUT 文件:

#include <sys/types.h>
#include "../DummyClass.h" //path to code being tested
#include "../../app.hxx"
#include "../../testClass.h"
#include "../mocks/dummyMock.h" //path to mock
#include "../mocks/AppMock.h"
#include "../mocks/testMock.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

using ::testing::AllOf;
using ::testing::Ge;
using ::testing::Le;

TEST(Test, testaplikacji)
{
    const char* a="abc";
    MockApplicationClass theClass;
}
int main(int argc, char** argv)
{
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

并生成文件:

CXX=g++

GTEST_DIR = ../../../UTframework/gmock-1.7.0/gtest
GMOCK_DIR = ../../../UTframework/gmock-1.7.0/


EXXX_PATH_PRE = /home/XXX/dev/exxx

# flags
CPPFLAGS += -isystem $(GTEST_DIR)/include -isystem $(GMOCK_DIR)/include 
CXXFLAGS += -g -Wall -Wextra -pthread
GMOCK_LIB = $(GMOCK_DIR)/make
# All tests produced by this Makefile.  Remember to add new tests you
TESTS = TestApp

CPPFLAGS += -isystem /home/XXX/dev/exxx/target/dataserver/include
CPPFLAGS += -isystem ../../Conflib/include
CPPFLAGS += -isystem /home/XXX/dev/exxx/target/XX/include
# All Google Test headers.  Usually you shouldn't change this
# definition.
GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h \
    $(GTEST_DIR)/include/gtest/internal/*.h

# All Google Mock headers. Note that all Google Test headers are
# included here too, as they are #included by Google Mock headers.
# Usually you shouldn't change this definition.
GMOCK_HEADERS = $(GMOCK_DIR)/include/gmock/*.h \
    $(GMOCK_DIR)/include/gmock/internal/*.h \
    $(GTEST_HEADERS)

all: $(TESTS)

clean:
rm -f $(TESTS) *.o

TestCIPApp: DummyUT.o DummyClass.o testClass.o
    $(CXX) $(CPPFLAGS) $(CXXFLAGS)  $^ -L$(GMOCK_LIB) -lgmock_main -o $@

DummyUT.o: srctest/DummyUT.cpp
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $^

DummyClass.o:   DummyClass.cpp
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $^ -o $@
copy:
    cp $(TESTS) ../../../UTBuildAll

最佳答案

如前所述,您必须为 App 添加另一个目标:

app.o:   app.cpp

而 TestCIPApp 必须依赖于此:

TestCIPApp: DummyUT.o DummyClass.o testClass.o app.o

现在目标文件也在那里,可以找到符号。

关于c++ - 对构造函数和析构函数的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25420080/

相关文章:

c# 检查线程池和其他线程

c++ - 如何在 GoogleTest 中使用 QTimers

c++ - 测试适用于成员变量的成员函数

c++ - 试图用多态来沮丧,出了什么问题?

c++ - 库位置 - Linux

c++ - C++ 中 typedef 和指针的规则是什么?

c++ - 在gtest中,如何将lib文件添加到可执行测试文件

c++ - 确定处理器对 SSE2 的支持?

c++ - c++ 使用哪个单元测试框架?

c# - 单元测试 WCF 错误