user246456 user246456 - 1 year ago 172
C++ Question

Can C++ export class from DLL

I would like to know if the export of class ( __declspec(dllexport) in VC++ ) is a kind of standard ( ANSI , ISO , ... )

I would like to know if someone has already try to do the same with intel c++ compiler and gcc ( mingw on windows ) and if it is possible to mix dlls generated from different compilers ( I really doubt that it is possible )


Answer Source

No, __declspec is VC++ specific.

One of the reasons that VC++ needs that is by default, DLLs do not expose symbols outside the DLL unless explicitly requested to do that. On Posix, shared objects expose all their (not-static) symbols unless explicitly told to hide them.


Based on your comment that you want to make your code portable, you want to use the preprocessor and do something like this:

#ifdef WIN32
    #define CLASS_FOO __declspec(dllexport)
    #define CLASS_FOO __declspec(dllimport) 
  #define CLASS_FOO

class CLASS_FOO foo
{ ... };

In the project implementing the class, make sure to add EXPORT_CLASS_FOO as a preprocessor definition (found in Project | NAME Properties.. under C/C++ | Preprocessor | Preprocess Definitions). This way, you'll export them when building the DLL, import them when you are using the DLL and do nothing special under Unix.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download