Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
162 views
in Technique[技术] by (71.8m points)

c++ - Unresolved Add_Test symbols, even with test fixture class included in the file

All of a sudden, after my tests had been working for hours, I'm getting errors that it can't find my text fixture functions (SetUp/TearDown). I'd had the fixture defined in a separate file, so as a quick fix I moved the class to the test file, but I still get the same problem!

Here's the current file. I didn't just omit the code for brevity, I've tried building it in this form to make sure the problem isn't somehow caused by the code inside the tests. I've left in the includes though.

#include "memory"
#include "../TestUtility/TestUtility.h"
#include "DependencyInjector.h"
#include "gtest/gtest.h"
#include "RecoverableErrorException.h"

namespace Test
{
    class DependencyInjectorTest : public testing::Test
    {
        public:
            DependencyInjectorTest();
            virtual ~DependencyInjectorTest();

        protected:
            virtual void SetUp() { Framework::DependencyInjector::Destroy(); };
            virtual void TearDown() override;
    };

    TEST_F ( DependencyInjectorTest, FindEmpty )
    { 
        // content ommitted 
    }

    TEST_F ( DependencyInjectorTest, Add )
    { 
        // content ommitted 
    }

    TEST_F ( DependencyInjectorTest, Find )
    { 
        // content ommitted 
    }
} // namespace Test

And here are the errors:

1>DependencyInjectorUnitTests.obj : error LNK2019: unresolved external symbol "public: __thiscall Test::DependencyInjectorTest::DependencyInjectorTest(void)" (??0DependencyInjectorTest@Test@@QAE@XZ) referenced in function "public: __thiscall Test::DependencyInjectorTest_Add_Test::DependencyInjectorTest_Add_Test(void)" (??0DependencyInjectorTest_Add_Test@Test@@QAE@XZ)
1>DependencyInjectorUnitTests.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall Test::DependencyInjectorTest::~DependencyInjectorTest(void)" (??1DependencyInjectorTest@Test@@UAE@XZ) referenced in function "public: virtual __thiscall Test::DependencyInjectorTest_Add_Test::~DependencyInjectorTest_Add_Test(void)" (??1DependencyInjectorTest_Add_Test@Test@@UAE@XZ)
1>DependencyInjectorUnitTests.obj : error LNK2001: unresolved external symbol "protected: virtual void __thiscall Test::DependencyInjectorTest::TearDown(void)" (?TearDown@DependencyInjectorTest@Test@@MAEXXZ)

UPDATE: The errors disappeared at some point. And then a few hours later, just as mysteriously and frustratingly, came back. Even on files I hadn't worked on since it was fine.

question from:https://stackoverflow.com/questions/65850189/unresolved-add-test-symbols-even-with-test-fixture-class-included-in-the-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to define the constructor, destructor and TearDown methods of DependencyInjectorTest. Even if they are virtual, the compiler needs basic methods to initialize the vtable.

https://en.cppreference.com/w/cpp/language/virtual

By declaring them in the class definition, the macros used them implicitly, and they need to be defined. As they're not, linking results in an undefined symbol error.

You can either remove the declarations or define the functions.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...