For simplicity, I've placed both DLL_TUTORIAL.dll with header MathFuncsDll.h in the root folder C:\ .
Then, created the empty project, setting
Configuration Properties->Linker->Input->Delay Loaded Dll's
to
C:\DLL_TUTORIAL.dll;%(DelayLoadDLLs)
and
Configuration Properties->VC++ Directories->Include Directories
to
C:\;$(IncludePath)
Complier commands:
/Zi /nologo /W3 /WX- /O2 /Oi /Oy- /GL /D "_MBCS" /Gm- /EHsc /MT /GS /Gy /fp:precise /Zc:wchar_t /Zc:forScope /Fp"Release\clean_rough_draft.pch" /Fa"Release\" /Fo"Release\" /Fd"Release\vc100.pdb" /Gd /analyze- /errorReport:queue
This project contains only the file with main.
main.cpp
#include <Windows.h>
#include <iostream>
#include "MathFuncsDll.h"
using namespace MathFuncs;
using namespace std;
int main()
{
std::cout<< MyMathFuncs<int>::Add(5,10)<<endl;
system("Pause");
return 0;
}
Dll has been compiled successfully in different solution.
MathFuncsDll.h
namespace MathFuncs
{
template <typename Type>
class MyMathFuncs
{
public:
static __declspec(dllexport) Type Add(Type a, Type b);
static __declspec(dllexport) Type Subtract(Type a, Type b);
static __declspec(dllexport) Type Multiply(Type a, Type b);
static __declspec(dllexport) Type Divide(Type a, Type b);
};
}
Definitions of these functions:
#include "MathFuncsDll.h"
#include <stdexcept>
using namespace std;
namespace MathFuncs
{
template <typename Type>
Type MyMathFuncs<Type>::Add(Type a,Type b)
{ return a+b; }
template <typename Type>
Type MyMathFuncs<Type>::Subtract(Type a,Type b)
{ return a-b; }
template <typename Type>
Type MyMathFuncs<Type>::Multiply(Type a,Type b)
{ return a*b; }
template <typename Type>
Type MyMathFuncs<Type>::Divide(Type a,Type b)
{
if(b == 0) throw new invalid_argument("Denominator cannot be zero!");
return a/b;
}
}
Running this program fails:
1>main.obj : error LNK2001: unresolved external symbol "public: static int __cdecl MathFuncs::MyMathFuncs::Add(int,int)" (?Add@?$MyMathFuncs@H@MathFuncs@@SAHHH@Z) 1>C:\Users\Tomek\Documents\Visual Studio 2010\Projects\clean_rough_draft\Release\clean_rough_draft.exe : fatal error LNK1120: 1 unresolved externals
Could you point out my mistake?
The problem has nothing to do with delayed loading of the DLL or not. I can see two problems here:
You are exporting templated functions. This wouldn't work that way because template exporting is not supported in Visual C++ compiler and however has already been dropped from the standard. For this to work you have two possible solutions:
extern template declarations in the header, etc. You can look that up in Google for more information, just search for 'extern template DLL' or something similar.You only export the methods when creating the DLL, but never import them (or at least that's what I see from the code). You use __declspec(dllexport) in front of each method, which tells the compiler to put that methods in the DLL. When you want to use these methods from a client application, you have to import them from the DLL. This is done by placing __declspec(dllimport) in front of each method. Since you can not put both prefixes on the methods you either have to create two almost the same header files that just differ on that method prefix thing or use some macro substitution based on whether this is a DLL building code or a client application. Once again, you can look that up in Google to see how its done.
I hope that helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With