尝试从外部dll使用函数时出现无法解析的外部符号错误!这是导出的dll中的代码:
//MathFunc.h
#pragma once
template <class T>
class MyMathFuncs
{
public:
T Add(T a, T b);
};
extern "C" {
MYAPI MyMathFuncs<int>* createInst(){
return new MyMathFuncs<int>;
}
}
//MathFunc.cpp
#include "MathFuncsDll.h"
#include <stdexcept>
using namespace std;
template <class T>
T MyMathFuncs<T>::Add(T a, T b)
{
return a + b;
}
编译此项目后,我得到了.dll和.lib文件。之后,我创建了一个新项目,并将.dll文件添加到输出目录,并将.lib文件添加到链接器->输入->其他依赖项。这是新项目中的代码:
//main.cpp
#include <iostream>
#include "MathFuncsDll.h"
using namespace std;
int main(){
MyMathFuncs<int> * pObj = createInst();
cout << pObj->Add(1, 1) << endl;
cin.get();
return 0;
}
但是,当我编译时会导致错误:
Error 1 error LNK2001: unresolved external symbol "public: int __thiscall MyMathFuncs<int>::Add(int,int)" (?Add@?$MyMathFuncs@H@@QAEHHH@Z) N:\Play around Code\DllApplication\DllApplication\main.obj
是因为我错误地导入了dll还是什么?我已经在新项目中检查了所有项目的设置,其中包括其他包含项和其他依赖项(.lib)。
在类定义中,您需要指定__declspec(dllexport)
在生成dll
和__declspec(dllimport)
使用时(我认为您正在使用)MYAPI
。
更改类定义,如下所示:
template <class T>
class MYAPI MyMathFuncs
{
public:
T Add(T a, T b);
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句