SangminKim
SangminKim

Reputation: 9166

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

Upvotes: 6

Views: 19852

Answers (2)

Mike Kinghan
Mike Kinghan

Reputation: 61620

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.

Upvotes: 4

Mark B
Mark B

Reputation: 96311

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.

Upvotes: 12

Related Questions