1
votes

recently I start study unit testing , and I want test my program with gtest. I install all with this order :

$ git clone https://github.com/google/googletest 
$ cd googletest 
$ cmake -DBUILD_SHARED_LIBS=ON . 
$ make 
$ cd googlemock 
$ sudo cp ./libgmock_main.so ./gtest/libgtest.so gtest/libgtest_main.so ./libgmock.so /usr/lib/ 
$ sudo ldconfig

and now write code :

#include "gtest/gtest.h"

class Add
{
  private:
     int element;
  public:
     Add():element(0){}
     ~Add(){}
     void setElement(int e){ element = e; }
     int getElement() { return element; }
     int adder(int e) { return element += e; }
};

class AddTest : public ::testing::Test
{
  protected:
       int abc(int a){
            return a;
        }

     virtual void SetUp(){
          add1.setElement(1);
          add2.setElement(20);
     }
     virtual void TearDown(){}

Add add1;
Add add2;
};

TEST_F(AddTest, getTest)
{
   EXPECT_EQ(1, add1.getElement());
   EXPECT_EQ(20, add2.getElement());
}

int main(int argc, char **argv)
{
   ::testing::InitGoogleTest(&argc, argv);
   return RUN_ALL_TESTS();
}

end when I run test i get this error :

CMakeFiles/mock2.dir/main.cpp.o: In function AddTest_getTest_Test::TestBody()': /home/artem/CLionProjects/mock2/main.cpp:33: undefined reference totesting::Message::Message()' /home/artem/CLionProjects/mock2/main.cpp:33: undefined reference to testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)' /home/artem/CLionProjects/mock2/main.cpp:33: undefined reference totesting::internal::AssertHelper::operator=(testing::Message const&) const' /home/artem/CLionProjects/mock2/main.cpp:33: undefined reference to testing::internal::AssertHelper::~AssertHelper()' /home/artem/CLionProjects/mock2/main.cpp:34: undefined reference totesting::Message::Message()' /home/artem/CLionProjects/mock2/main.cpp:34: undefined reference to testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)' /home/artem/CLionProjects/mock2/main.cpp:34: undefined reference totesting::internal::AssertHelper::operator=(testing::Message const&) const' /home/artem/CLionProjects/mock2/main.cpp:34: undefined reference to testing::internal::AssertHelper::~AssertHelper()' /home/artem/CLionProjects/mock2/main.cpp:33: undefined reference totesting::internal::AssertHelper::~AssertHelper()' /home/artem/CLionProjects/mock2/main.cpp:34: undefined reference to testing::internal::AssertHelper::~AssertHelper()' CMakeFiles/mock2.dir/main.cpp.o: In functionmain': /home/artem/CLionProjects/mock2/main.cpp:39: undefined reference to testing::InitGoogleTest(int*, char**)' CMakeFiles/mock2.dir/main.cpp.o: In function__static_initialization_and_destruction_0(int, int)': /home/artem/CLionProjects/mock2/main.cpp:31: undefined reference to testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, testing::internal::CodeLocation, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)' CMakeFiles/mock2.dir/main.cpp.o: In functionRUN_ALL_TESTS()': /usr/local/include/gtest/gtest.h:2235: undefined reference to testing::UnitTest::GetInstance()' /usr/local/include/gtest/gtest.h:2235: undefined reference totesting::UnitTest::Run()' CMakeFiles/mock2.dir/main.cpp.o: In function AddTest::AddTest()': /home/artem/CLionProjects/mock2/main.cpp:15: undefined reference totesting::Test::Test()' CMakeFiles/mock2.dir/main.cpp.o: In function AddTest::~AddTest()': /home/artem/CLionProjects/mock2/main.cpp:15: undefined reference totesting::Test::~Test()' CMakeFiles/mock2.dir/main.cpp.o: In function testing::internal::scoped_ptr<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::reset(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*)': /usr/local/include/gtest/internal/gtest-port.h:1172: undefined reference totesting::internal::IsTrue(bool)' CMakeFiles/mock2.dir/main.cpp.o: In function testing::internal::scoped_ptr<std::__cxx11::basic_stringstream<char, std::char_traits<char>, std::allocator<char> > >::reset(std::__cxx11::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >*)': /usr/local/include/gtest/internal/gtest-port.h:1172: undefined reference totesting::internal::IsTrue(bool)' CMakeFiles/mock2.dir/main.cpp.o: In function testing::AssertionResult testing::internal::CmpHelperEQ<int, int>(char const*, char const*, int const&, int const&)': /usr/local/include/gtest/gtest.h:1394: undefined reference totesting::AssertionSuccess()' CMakeFiles/mock2.dir/main.cpp.o: In function testing::AssertionResult testing::internal::CmpHelperEQFailure<int, int>(char const*, char const*, int const&, int const&)': /usr/local/include/gtest/gtest.h:1384: undefined reference totesting::internal::EqFailure(char const*, char const*, std::__cxx11::basic_string, std::allocator > const&, std::__cxx11::basic_string, std::allocator > const&, bool)' CMakeFiles/mock2.dir/main.cpp.o:(.rodata._ZTI7AddTest[_ZTI7AddTest]+0x10): undefined reference to `typeinfo for testing::Test' collect2: error: ld returned 1 exit status CMakeFiles/mock2.dir/build.make:94: recipe for target 'mock2' failed make[3]: * [mock2] Error 1 CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/mock2.dir/all' failed make[2]: [CMakeFiles/mock2.dir/all] Error 2 CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/mock2.dir/rule' failed make[1]: [CMakeFiles/mock2.dir/rule] Error 2 Makefile:118: recipe for target 'mock2' failed make: * [mock2] Error 2

but when use command

g++ main.cpp -o test -lgtest -lpthread

everytheng is good. How can I fix it and run it not in command line ?

1

1 Answers

0
votes

If you are using CLion then you may have a CMakeLists.txt file you should add rules to link to the libraries. To do this add the following line to your CMakeLists.txt

enable_testing()
find_package(GTest REQUIRED) # Find the google testing framework on your system
include_directories(${GTEST_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${GTEST_LIBRARIES}) # Replace ${PROJECT_NAME} with your target name

For more details go here.