Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is __declspec(dllexport) needed in cpp files

Tags:

c++

windows

dll

Probably a simple question but I only have Linux to test this code on where __declspec(dllexport) is not needed. In the current code __declspec(dllexport) is in front of all files in the .h file but just in front of like 50% of the functions in the cpp file so I am wondering if they are really needed in the cpp file at all ?

like image 781
Zitrax Avatar asked Feb 17 '09 15:02

Zitrax


2 Answers

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.

like image 126
Doug T. Avatar answered Sep 22 '22 20:09

Doug T.


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

like image 20
Vinay Avatar answered Sep 21 '22 20:09

Vinay