对构造函数和析构函数的未定义引用

Undefined reference to both Conctructor and destructor

本文关键字:未定义 引用 析构函数 构造函数      更新时间:2023-10-16

我正在尝试使用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_ */

here 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();
}

And Make file:

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

现在objectfile也在那里,可以找到符号