链接gtest项目

时间:2018-01-03 09:31:30

标签: c++ googletest clion

最近我开始学习单元测试,我想用gtest测试我的程序。我用这个订单安装所有:

$ 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

现在编写代码:

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

当我运行测试时结束我收到此错误:

CMakeFiles / mock2.dir / main.cpp.o:在函数AddTest_getTest_Test::TestBody()': /home/artem/CLionProjects/mock2/main.cpp:33: undefined reference to中测试:: Message :: Message()&#39; /home/artem/CLionProjects/mock2/main.cpp:33:未定义引用testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)' /home/artem/CLionProjects/mock2/main.cpp:33: undefined reference to testing :: internal :: AssertHelper :: operator =(testing :: Message const&amp;)const&#39; /home/artem/CLionProjects/mock2/main.cpp:33:对testing::internal::AssertHelper::~AssertHelper()' /home/artem/CLionProjects/mock2/main.cpp:34: undefined reference to测试的未定义引用:: Message :: Message()&#39; /home/artem/CLionProjects/mock2/main.cpp:34:对testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)' /home/artem/CLionProjects/mock2/main.cpp:34: undefined reference to testing :: internal :: AssertHelper :: operator =(testing :: Message const&amp;)const&#39;的未定义引用 /home/artem/CLionProjects/mock2/main.cpp:34:未定义引用testing::internal::AssertHelper::~AssertHelper()' /home/artem/CLionProjects/mock2/main.cpp:33: undefined reference to testing :: internal :: AssertHelper :: ~AssertHelper()&#39; /home/artem/CLionProjects/mock2/main.cpp:34:未明确引用testing::internal::AssertHelper::~AssertHelper()' CMakeFiles/mock2.dir/main.cpp.o: In function main&#39;: /home/artem/CLionProjects/mock2/main.cpp:39:未定义引用testing::InitGoogleTest(int*, char**)' CMakeFiles/mock2.dir/main.cpp.o: In function __ static_initialization_and_destruction_0(int,int)&#39;: /home/artem/CLionProjects/mock2/main.cpp:31:对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 function RUN_ALL_TESTS()&#39;的未定义引用: /usr/local/include/gtest/gtest.h:2235:对testing::UnitTest::GetInstance()' /usr/local/include/gtest/gtest.h:2235: undefined reference to测试的未定义引用:: UnitTest :: Run()&#39; CMakeFiles / mock2.dir / main.cpp.o:在函数AddTest::AddTest()': /home/artem/CLionProjects/mock2/main.cpp:15: undefined reference to中测试:: Test :: Test()&#39; CMakeFiles / mock2.dir / main.cpp.o:在函数AddTest::~AddTest()': /home/artem/CLionProjects/mock2/main.cpp:15: undefined reference to中测试:: Test :: ~Test()&#39; CMakeFiles / mock2.dir / main.cpp.o:在函数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 to中测试:: internal :: IsTrue(bool)&#39; CMakeFiles / mock2.dir / main.cpp.o:在函数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 to中测试:: internal :: IsTrue(bool)&#39; CMakeFiles / mock2.dir / main.cpp.o:在函数testing::AssertionResult testing::internal::CmpHelperEQ<int, int>(char const*, char const*, int const&, int const&)': /usr/local/include/gtest/gtest.h:1394: undefined reference to中测试:: AssertionSuccess()&#39; CMakeFiles / mock2.dir / main.cpp.o:在函数testing::AssertionResult testing::internal::CmpHelperEQFailure<int, int>(char const*, char const*, int const&, int const&)': /usr/local/include/gtest/gtest.h:1384: undefined reference to中测试:: internal :: EqFailure(char const *,char const *,std :: __ cxx11 :: basic_string,std :: allocator&gt; const&amp; amp ;,std :: __ cxx11 :: basic_string,std :: allocator&gt; const&amp;,bool)&#39; CMakeFiles / mock2.dir / main.cpp.o:(.rodata._ZTI7AddTest [_ZTI7AddTest] + 0x10):未定义引用`typeinfo for testing :: Test&#39; collect2:错误:ld返回1退出状态 CMakeFiles / mock2.dir / build.make:94:目标&#39; mock2&#39;失败 make [3]: * [mock2]错误1 CMakeFiles / Makefile2:67:目标的配方&#39; CMakeFiles / mock2.dir / all&#39;失败 make [2]: [CMakeFiles / mock2.dir / all]错误2 CMakeFiles / Makefile2:79:目标的配方&#39; CMakeFiles / mock2.dir / rule&#39;失败 make [1]: [CMakeFiles / mock2.dir / rule]错误2 Makefile:118:目标&#39; mock2&#39;失败 make:* [mock2]错误2

但是当使用命令

g++ main.cpp -o test -lgtest -lpthread
每一个人都很好。如何修复它并在命令行中运行它?

1 个答案:

答案 0 :(得分:0)

如果您正在使用CLion,那么您可能有一个CMakeLists.txt文件,您应该添加规则以链接到库。为此,请将以下行添加到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

有关详细信息,请转到here