Google test linking error as method definition not found

1,297 views
Skip to first unread message

Naveen Kh

unread,
Nov 26, 2018, 6:58:34 AM11/26/18
to Google C++ Testing Framework
I have a class called skiplist which has 3 public virtual methods and which are defined in the .cpp file. 
I have a test class called TestSkipList which is used for gmock test. And I am trying to test the real class object instead of a mock object.
But this is giving me linking error saying that Insert methods definition is not found, Please suggest me what might be the problem.


class SkipList {

public:
virtual bool Insert(int key, T value);

virtual bool Remove(int key);
virtual bool Lookup(int key);

virtual ~SkipList() {
}
}

class
TestSkipList : public ::testing::Test {
protected:
TestSkipList() {}

virtual ~TestSkipList() {}

virtual void SetUp() {
S = SkipList<string>();
}

virtual void TearDown() {
}
SkipList <string>S;
};

TEST_F(TestSkipList, Insertion) {
EXPECT_EQ(1,S.Insert(2,"nkh"));
}


int main(int argc, char** argv) {
// The following line must be executed to initialize Google Mock
// (and Google Test) before running the tests.
::testing::InitGoogleMock(&argc, argv);
return RUN_ALL_TESTS();



CMakeFiles/test_skiplist.dir/test_skiplist.cpp.o: In function `TestSkipList_Insertion_Test::TestBody()':
/.../test/test_skiplist.cpp:29: undefined reference to `SkipList<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::Insert(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
CMakeFiles/test_skiplist.dir/test_skiplist.cpp.o:(.rodata._ZTV8SkipListINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE[_ZTV8SkipListINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE]+0x10): undefined reference to `SkipList<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::Insert(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
CMakeFiles/test_skiplist.dir/test_skiplist.cpp.o:(.rodata._ZTV8SkipListINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE[_ZTV8SkipListINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE]+0x18): undefined reference to `SkipList<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::Remove(int)'
CMakeFiles/test_skiplist.dir/test_skiplist.cpp.o:(.rodata._ZTV8SkipListINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE[_ZTV8SkipListINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE]+0x20): undefined reference to `SkipList<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::Lookup(int)'
collect2: error: ld returned 1 exit status
}

David Wallace

unread,
Dec 1, 2018, 4:34:49 AM12/1/18
to Google C++ Testing Framework
This is just C++ issues, not specific to googletest.  What you've shown us here is just the declarations for the methods of SkipList.  You say that the methods are defined in a (separate) .cpp file, which needs to be linked.  But another issue is that you have templated code - even if you are linking it here, the other file won't compile a version of SkipList<string> unless something in that file invokes that specialized version of the class.  For our templated code, we generally have the definitions of all the methods in the .h file that defines the templates - that way, they will compile whatever specialized version is needed when the code is used.

Dave W.
Reply all
Reply to author
Forward
0 new messages