实现C+时出现错误LNK2019+;用于DLL的类 我实现了一个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) ();
我想,我的上一次更新应该能起作用。但是对于实现C+时出现错误LNK2019+;用于DLL的类 我实现了一个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) ();,c++,dll,C++,Dll,我想,我的上一次更新应该能起作用。但是对于loadData()和~ASM(),仍然存在链接器错误LNK2019。我在同一个解决方案中完成了测试项目和ASM_Lib项目。可能有什么问题?DLL仅导出CreateASMObject函数。ASM::loadData不导出,但在测试应用程序中使用。 我可以提出两种解决方法: 1) 属性仅包含整个ASM类或loadData成员,并将ASM_Lib.Lib添加到测试应用程序项目中 2) 声明纯抽象类(接口)IASM并更改CreateASMObject的返回类
loadData()
和~ASM()
,仍然存在链接器错误LNK2019。我在同一个解决方案中完成了测试项目和ASM_Lib项目。可能有什么问题?DLL仅导出CreateASMObject函数。ASM::loadData不导出,但在测试应用程序中使用。
我可以提出两种解决方法:
1) 属性仅包含整个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并实现Absact方法(可以在CPP文件中完成)。
在这种情况下,链接器不需要loadData方法的地址,因为它将在运行时通过vtable解析
另外,您必须确保DLL及其客户端使用相同的堆管理器实例(例如,相同版本的C运行时DLL)。Otherwize为在另一个模块中创建的对象调用delete是不安全的。解决方案是添加一个实现删除的方法:
class IASM
{
public:
virtual void destroy() = 0;
protected:
~IMyAPI() = default;
};
class ASM: public IASM
{
public:
virtual void destroy() override
{
delete this;
}
};
loadData没有实现,只是声明了?@LPs;抱歉,我编辑了,我在cpp文件中实现了。如果要动态加载dll,为什么要链接到库?对于您的错误,\u tmain可能会转换为用于unicode构建的wmain。不知道,我从不使用tchar的东西。有两个问题:在
ASM_Lib.cpp
中实现的函数是在命名空间VIDEO_ANALYTICS_PLATFORM
中定义的。但是,在.h文件中,我没有看到在该名称空间下声明它们。从错误消息中不清楚您是否正在链接创建.dll所产生的.lib。我是按照选项2中的建议实现的。这个链接()说你的方法是成熟的方法。但我仍然有错误。让我进一步调查。我在第一篇文章中更新为编辑。我想,我的上一次更新应该可以,它和你建议的一样。但是对于loadData()
和~ASM()
,仍然存在链接器错误LNK2019。我在同一个解决方案中完成了测试项目和ASM_Lib项目。有什么问题吗?如果我在test.cpp中注释掉这两行,它会起作用。因此,这意味着类已加载,但方法未正确加载。1)i_ASM析构函数不是抽象的。2) 显式调用析构函数是不正确的,请改为调用delete pASM
。谢谢,我弄错了。不是说没问题,而是应该行得通,对吧?错误仍然存在。
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
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;
}
class IASM
{
public:
virtual ~IASM() = 0;
virtual int loadData(string path, string ext) = 0;
};
extern "C"
{
DLLCLASS IASM* _cdecl CreateASMObject();
};
class IASM
{
public:
virtual void destroy() = 0;
protected:
~IMyAPI() = default;
};
class ASM: public IASM
{
public:
virtual void destroy() override
{
delete this;
}
};