Is __declspec(dllexport) needed in cpp files

No, its only needed in the header.

Here's a link with more info.

Expanding on what Vinay was saying, I've often seen a macro defined

#if defined(MODULENAME_IMPORT)
#define EXPORTED __declspec(dllimport)
#elif defined(MODULENAME_EXPORT)
#define EXPORTED __declspec(dllexport)
#endif

Then in your header you do

void EXPORTED foo();

set the defines accordingly in the project settings for the project doing the import/exporting.


No, it is not required in cpp file. Only in declaration it is required.

For Example if I have a class CMyClass. If I want to export this then .h will have

.h Server code

__declspec(dllexport) CMyClass { };

In the client code i.e., which uses this class you have to forward declare the class as

Client code

__declspec(dllimport) CMyClass;

// Code to use the class


You may use in .cpp file also when you have templated code and you are instantiating in .cpp file then you need to export the definition when it is instantiated. But even in this case, I have seen that doing in .h also works. On windows you can use dumpbin.exe /exports *.dll to see what signatures are exported, there is similar utility in Linux too. This will give you an idea how signature is exported.

Tags:

Windows

C++

Dll