Requires __declspec (dllexport) in cpp files

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

+7
c ++ windows dll
source share
3 answers

No, it is only needed in the title.

Here is a link with additional information.

Turning around what Vinay said, I often saw a macro defined

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

Then in your title you do

 void EXPORTED foo(); 

Set the appropriate parameters in the project settings for the project that imports / exports.

+11
source share

No, this is not required in the cpp file. Only the declaration is required.

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

.h server code

__ declspec (dllexport) CMyClass {};

In client code, i.e. that uses this class, you must forward the class declaration as

Client code

__ declspec (dllimport) CMyClass;

// Code for using the class

+5
source share

You can also use the .cpp file when you have the boilerplate code and you create an instance in the .cpp file, then you need to export the definition when you create it. But even so, I saw that working in .h also works. In windows, you can use dumpbin.exe / exports * .dll to see which subscriptions are exported; Linux has a similar utility. This will give you an idea of ​​how the signature is exported.

+2
source share

All Articles