奇怪的c ++链接器错误

时间:2017-09-27 08:20:50

标签: c++ linker

我有一个可执行文件,使用google test lib执行测试。

我的test.exe链接到:

  • 核心
  • GTEST

现在,在Core库中,我有一个共享指针(模板)类。据我了解,模板类不需要dllexport / import装饰器(通过添加它确认并且仍然没有运气)在类定义上。

SharedPointer.h

template<typename T>
struct TSharedPtr
{
private:
    T* Ptr = nullptr;       /**< Native pointer to data */
    RefCounter* Counter = nullptr;      /**< Refernce counter to data */
    uint32 GetRefCount() const
    {
        return Counter->GetCount();
    }
};

链接器错误与RefCounter-> GetCount()有关,它表示无法找到该函数。

这是我的RefCounter.h

#pragma once
#include "CoreAPI.h"
class CORE_API RefCounter
{
private:
    uint32 Count = 0;
public:
    inline void AddRef() { Count++; }
    inline uint32 RemoveRef() { return --Count;}
    inline uint32 GetCount() { return Count; }
};

正如您所看到的,我添加了CORE_API(dllimport / export)欺骗器,但无济于事我收到以下链接器错误:

> Error LNK2019 unresolved external symbol "__declspec(dllimport) public: __thiscall Core::RefCounter::RefCounter(void)" (__imp_??0RefCounter@Core@@QAE@XZ) referenced in function "public: __thiscall Core::TSharedPtr<class TestClass>::TSharedPtr<class TestClass>(class TestClass *)" (??0?$TSharedPtr@VTestClass@@@Core@@QAE@PAVTestClass@@@Z) RunTests    C:\Users\<USERNAME>\Project\RunTests\TSharedPtrTests.obj    1   

> Error LNK2019 unresolved external symbol "__declspec(dllimport) public: void __thiscall Core::RefCounter::AddRef(void)" (__imp_?AddRef@RefCounter@Core@@QAEXXZ) referenced in function "public: __thiscall Core::TSharedPtr<class TestClass>::TSharedPtr<class TestClass>(struct Core::TSharedPtr<class TestClass> const &)" (??0?$TSharedPtr@VTestClass@@@Core@@QAE@ABU01@@Z)    RunTests    C:\Users\<USERNAME>\Project\RunTests\TSharedPtrTests.obj    1   

> Error LNK2019 unresolved external symbol "__declspec(dllimport) public: unsigned int __thiscall Core::RefCounter::RemoveRef(void)" (__imp_?RemoveRef@RefCounter@Core@@QAEIXZ) referenced in function "public: __thiscall Core::TSharedPtr<class TestClass>::~TSharedPtr<class TestClass>(void)" (??1?$TSharedPtr@VTestClass@@@Core@@QAE@XZ)   RunTests    C:\Users\<USERNAME>\Project\RunTests\TSharedPtrTests.obj    1   

> Error LNK2019 unresolved external symbol "__declspec(dllimport) public: unsigned int __thiscall Core::RefCounter::GetCount(void)" (__imp_?GetCount@RefCounter@Core@@QAEIXZ) referenced in function "public: unsigned int __thiscall Core::TSharedPtr<class TestClass>::GetRefCount(void)const " (?GetRefCount@?$TSharedPtr@VTestClass@@@Core@@QBEIXZ) RunTests    C:\Users\<USERNAME>\Project\RunTests\TSharedPtrTests.obj    1   

> Error LNK1120 4 unresolved externals  RunTests    C:\Users\<USERNAME>\Binaries\Windows\Runtime\Editor\Debug\RunTests.exe  1   

注意::如果我从RefCounter中删除“CORE_API”,它会编译并运行。但我认为dllimport / export装饰器是构建DLL的具体类所必需的。任何人都可以解释为什么会这样,因为它对我来说没有多大意义......

0 个答案:

没有答案