Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gtest, undefined reference to 'testing::Test::~Test()', testing::Test::Test()

i installed the gtest using apt-get install libgtest-dev

and i am trying to check if that is working or not.

so i make simple code for testing in eclipse.

but there are error,

undefined reference to 'testing::Test::~Test()'
undefined reference to 'testing::Test::Test()'

conversely if i change the inheritance at the ATest class as protected the error disappear but

the other error occur

testing::Test is inaccessible base of 'ATest_AAA_Test'

what is wrong ?

#include <iostream>
#include <gtest/gtest.h>

class A{
public:
    int a;
    A(int a){this->a = a;}
    A(){}
    ~A(){}
    int getA(){return a;}
    void setA(int a){this->a = a;}
};

class ATest : public ::testing::Test{
public:
    virtual void SetUp(){
        a1 = A(1);
        a2 = A(2);
        a3 = A(3);
    }
    virtual void TearDwon(){}
    A a1;
    A a2;
    A a3;
};

TEST_F(ATest, AAA){
    EXPECT_EQ(1, a1.getA());
    EXPECT_EQ(2, a2.getA());
    EXPECT_EQ(3, a3.getA());
}

int main(int argc, char **argv){
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}
like image 834
SangminKim Avatar asked Jun 10 '14 15:06

SangminKim


People also ask

How do I turn off Test Gtest?

If you have a broken test that you cannot fix right away, you can add the DISABLED_ prefix to its name. This will exclude it from execution. This is better than commenting out the code or using #if 0 , as disabled tests are still compiled (and thus won't rot).

Is Gtest header only?

Google Test is not header-only: there are libraries to build. So, as a Visual Studio user, you have essentially two options.


2 Answers

I know this sounds obvious but my psychic debugging skills tell me you forgot to add -lgtest after your object file name when you linked your final binary.

like image 147
Mark B Avatar answered Sep 28 '22 03:09

Mark B


Installing libgtest-dev does not install any of the gtest binaries. It simply installs the googletest source package on your system - headers under /usr/include/gtest and source files under /usr/src/gtest, where you could proceed to build it with cmake or GNU autotools if you want.

There is no binary package for googletest in ubuntu/debian software channels (or elsewhere that I know of). The normal practice is to download the source archive, extract it and use binaries built by yourself. The README in the source package gives guidance on building and using the library.

There is normally no purpose in performing a system install of the source package, as you have done.

The linkage error you have encountered:

undefined reference to 'testing::Test::~Test()

has nothing to do with your code: it occurs because you are not linking libgtest, and you cannot link it because it is not installed by libgtest-dev and you have not built it yourself.

This linkage error disappears when you change the code in a way that introduces a compilation error, because if compilation fails then linkage does not happen.

like image 34
Mike Kinghan Avatar answered Sep 28 '22 01:09

Mike Kinghan