为DLL实现C++类时出现错误LNK2019

error LNK2019 in implementing a C++ class for DLL

本文关键字:错误 LNK2019 DLL 实现 C++      更新时间:2023-10-16

我将C++类实现为DLL。但当我连接到dll时,我有链接器错误。我有正确的设置,不确定出了什么问题。我进行了研究,但找不到相关的解决方案,所以我提出了质疑。

ASM_Lib.h
    #ifdef EXPORT
    #define DLLCLASS __declspec(dllexport)
    #else
    #define DLLCLASS __declspec(dllimport)
    #endif
    class ASM
    {
        public:
            ASM();
            ~ASM();
            int loadData(string path, string ext);
            int landmarkEqualization();
        private:
            vector<PtsData_<CurrentType_>> pts;//this vector size is same as number of images, released after use
            vector<string> files;//file names
            vector<TrainingData_<CurrentType_>> td;//this vector size is same as number of images, released after use
            vector<Mat> images;
    };
    extern "C" /*Important for avoiding Name decoration*/
    {
        DLLCLASS  ASM* _cdecl CreateASMObject();
    };
    // Function Pointer Declaration of CreateASMObject() [Entry Point Function]
    typedef ASM* (*CREATE_ASM) ();

ASM_Lib.cpp

namespace VIDEO_ANALYTICS_PLATFORM{
    DLLCLASS ASM* _cdecl CreateASMObject() {
        return new ASM();
    }
    ASM::ASM()
    {
    }
    ASM::~ASM()
    {
    }
    int ASM::loadData(string path, string ext)
    {
            return FILE_READ_WRITE_ERROR;
    }
    ///*
    //This loop equalize all landmark points to 
    //be equal distances
    //*/
    int ASM::landmarkEqualization()
    {
        //Clear vector
        pts.clear();
        vector<PtsData_<CurrentType_>>().swap(pts);
        return SUCCESS;
    }

}

然后在我的测试程序中,我作为接口

#include "stdafx.h"
#include <iostream>
#include "ASM_Lib.h"
using namespace VIDEO_ANALYTICS_PLATFORM;
int _tmain(int argc, _TCHAR* argv[])
{
    HINSTANCE hDLL = LoadLibrary(L"ASM_Lib.dll"); 
    if (hDLL == NULL){
        std::cout << "Failed to load library.n";
    }else{
        CREATE_ASM pEntryFunction = (CREATE_ASM)GetProcAddress(hDLL, "CreateASMObject");
        ASM* pASM = pEntryFunction();
        if (pASM) {                             
            pASM->loadData("C:\PointsFiles", "pts");                         
        }
        FreeLibrary(hDLL);
    }
    std::cin.get();
    return 0;
}

但我有LNK2019链接错误(在函数wmain中引用),我没有wmain。可能是什么问题?

test.obj : error LNK2019: unresolved external symbol "public: int __cdecl VIDEO_ANALYTICS_PLATFORM::ASM::loadData(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?loadData@ASM@VIDEO_ANALYTICS_PLATFORM@@QEAAHV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0@Z) referenced in function wmain

如果我注释掉这个API pASM->loadData("C:\PointsFiles", "pts");,那么它可以正常工作,并且可以正确加载dll。

编辑:上次更新

ASM_Lib.h
#ifdef EXPORT
#define DLLCLASS __declspec(dllexport)
#else
#define DLLCLASS __declspec(dllimport)
#endif
namespace VIDEO_ANALYTICS_PLATFORM{
    class i_ASM
    {
       public:
          virtual ~i_ASM(){ ; };
          virtual int loadData(string path, string ext)=0;
          virtual int landmarkEqualization() = 0;
    };
    class ASM : public i_ASM
    {
        public:
            ASM(){ }
            int loadData(string path, string ext);
            int landmarkEqualization();
        private:
            vector<PtsData_<CurrentType_>> pts;//this vector size is same as number of images, released after use
            vector<string> files;//file names
            vector<TrainingData_<CurrentType_>> td;//this vector size is same as number of images, released after use
            vector<Mat> images;
    };
    extern "C" 
    {
        DLLCLASS  i_ASM* _cdecl CreateASMObject();
    };
}
ASM_Lib.cpp
namespace VIDEO_ANALYTICS_PLATFORM{
    DLLCLASS i_ASM* _cdecl CreateASMObject() {
        return new ASM();
    }   
    int ASM::loadData(string path, string ext)
    {
         return 0;
    }
    ///*
    //This loop equalize all landmark points to 
    //be equal distances
    //*/
    int ASM::landmarkEqualization()
    {
        //Clear vector
        pts.clear();
        vector<PtsData_<CurrentType_>>().swap(pts);
        return SUCCESS;
    }
}
Test.cpp
#include "ASM_Lib.h"
using namespace VIDEO_ANALYTICS_PLATFORM;
int _tmain(int argc, _TCHAR* argv[])
{       
     ASM* pASM = ::CreateASMObject();
     if (pASM) {
         pASM->loadData("C:\PointsFiles", "pts");
         pASM->~ASM();
         pASM = NULL;
     }
    return 0;
}

我认为,我的最后一次更新应该有效。但是对于loadData()~ASM()仍然存在链接器错误LNK2019。我在同一个解决方案中完成了测试项目和ASM_Lib项目。可能出了什么问题?

DLL仅导出CreateASMObject函数。ASM::loadData未导出,但在测试应用程序中使用。我可以提出2种修复方法:

1) 仅使用__declspec(dllexport)属性整个ASM类或loadData成员,并将ASM_Lib.Lib添加到测试应用程序项目中。

2) 声明纯抽象类(接口)IASM并更改CreateASMObject:的返回类型

class IASM
{
public:
  virtual ~IASM() = 0;
  virtual int loadData(string path, string ext) = 0;
};
extern "C"
{
  DLLCLASS  IASM* _cdecl CreateASMObject();
};

然后从IASM派生ASM并实现Abstract方法(可以在CPP文件中完成)。在这种情况下,链接器不需要loadData方法的地址,因为它将在运行时通过vtable解析。

PS。您必须确保DLL及其客户端使用相同的堆管理器实例(例如,相同版本的C运行时DLL)。否则,为在另一个模块中创建的对象调用delete是不安全的。解决方案是添加一种实现删除的方法:

class IASM
{
public:
  virtual void destroy() = 0;
protected:
  ~IMyAPI() = default;
};    
class ASM: public IASM
{
public:
  virtual void destroy() override
  {
    delete this;
  }
};