gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:56:32 25 4
gpt4 key购买 nike

我将 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;
}


}

然后在我的测试程序中,我的接口(interface)为

#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) 声明纯抽象类(接口(interface))IASM并改变CreateASMObject的返回类型:

class IASM
{
public:
virtual ~IASM() = 0;
virtual int loadData(string path, string ext) = 0;
};

extern "C"
{
DLLCLASS IASM* _cdecl CreateASMObject();
};

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

附言。您必须确保 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;
}
};

关于c++ - 为 DLL 实现 C++ 类时出现错误 LNK2019,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28448409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com