Compiler error while using Dll

Hi,

I am learning how to build and use a dll.

In Visual Studio 2010, I created a dll project.

Added header file
MathDll.h
1
2
3
4
5
6
7
#include <iostream>

class MathDll
{
public:
	static __declspec(dllexport) int Sum(int, int);
};


Added cpp file
1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;

#include "MathDll.h"


int MathDll::Sum(int a, int b)
{
	return a+b;
}


I then built the solution.
It generated MathDll.dll

I copied MathDll.dll, MathDll.lib and MathDll.h and pasted in the project folder of a new console application.

This console application would use the above dll.
I added the MathDll.h header file in its Header folder
Created a main.cpp
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include "MathDll.h"

using namespace std;

int main()
{
	
	int c = MathDll::Sum(5, 7);
	cout << c << endl;
}


When I build this project, its giving the following error

1>main.obj : error LNK2019: unresolved external symbol "public: static int __cdecl MathDll::Sum(int,int)" (?Sum@MathDll@@SAHHH@Z) referenced in function _main
Debug\MathTest.exe : fatal error LNK1120: 1 unresolved externals
1>
1>Build FAILED.

Please advice what I am doing wrong here.

Thank you
I
Any help greatly appreciated :)
The problem is dllexport. When using in a non dll it needs to be dllimport. See:

http://stackoverflow.com/questions/57999/what-is-the-difference-between-dllexport-and-dllimport

You can use macros to make the difference:

1
2
3
4
5
6
7
8
9
10
11
#ifdef COMPILE_DLL
#define DLL_PORT __declspec( dllexport )
#else
#define DLL_PORT __declspec( dllimport )
#endif

class MathDll
{
public:
	static DLL_PORT int Sum(int, int);
};
Thank you very much. It worked.
Topic archived. No new replies allowed.