Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export a C++ class from a dll? [duplicate]

I have a class which has two overloaded functions. How do I export it from a dll and also how to use it by other C++ classes? My class looks like this:

#define DECLDIREXP __declspec(dllexport)   #define DECLDIRIMP __declspec(dllimport)   class DECLDIREXP xyz   {  public:            void printing();           void printing(int a); };    using namespace std;   void xyz::printing() {         cout<<"hello i donot take any argument"; }   void xyz::printing(int a) {         cout<<"hello i take "<< a <<"as argument"; } 
like image 674
Apoorva sahay Avatar asked Jul 27 '11 07:07

Apoorva sahay


People also ask

Can DLL export classes?

You can declare C++ classes with the dllimport or dllexport attribute. These forms imply that the entire class is imported or exported. Classes exported this way are called exportable classes.

What does __ Declspec Dllexport do?

__declspec(dllexport) adds the export directive to the object file so you do not need to use a . def file. This convenience is most apparent when trying to export decorated C++ function names.

What does __ Declspec Dllimport mean?

__declspec(dllimport) is a storage-class specifier that tells the compiler that a function or object or data type is defined in an external DLL. The function or object or data type is exported from a DLL with a corresponding __declspec(dllexport) .


1 Answers

A common approach is to have a single macro (let's call it EXPORT) which either expands to dllimport or dllexport depending on whether some sort of "building the DLL right now" define is set, like this:

#ifdef MAKEDLL #  define EXPORT __declspec(dllexport) #else #  define EXPORT __declspec(dllimport) #endif  class EXPORT xyz {   // ... }; 

The idea is that when building your DLL, you add MAKEDLL to the preprocessor definitions. That way, all the code will be exported. Clients who link against your DLL (and hence include this header file) don't need to do anything at all. By not defining MAKEDLL, they will automatically import all the code.

The advantage of this approach is that the burden of getting the macros right is moved from the many (the clients) to just the author of the DLL.

The disadvantage of this is that when using the code above as it is, it's no longer possible to just compile the code directly into some client module since it's not possible to define the EXPORT macro to nothing. To achieve that, you'd need to have another check which, if true, defines EXPORT to nothing.

On a slightly different topic: in many cases, it's not possible (or desired!) to export a complete class like that. Instead, you may want to just export the symbols you need. For instance, in your case, you may want to just export the two public methods. That way, all the private/protected members won't be exported:

class xyz { public:      EXPORT void printing();     EXPORT void printing(int a); }; 
like image 189
Frerich Raabe Avatar answered Sep 23 '22 11:09

Frerich Raabe