1

I have a bunch of classes like this:

class A 
{
public:
   std::string s;
   std::vector<int> v;
   int a;
}

That I'm trying to export to be used in a dll. I want clients to be able to use the class like this:

A a;
a.s = "abc";
b.v.push_back(4);

But because vector and string cannot be exported, this isn't possible. Besides providing getters and setters for each member and using an abstract interface that has those getters and setters, is there any way around this?

Michael Hagar
  • 626
  • 5
  • 20
  • 1
    While redesigning something, try to actually come up with a sensible design. A bunch of public data members, either bare or decorated with getters/setters, isn't. If you are trying to work around the supposed MSVC inability to export strings, [which isn't really that bad](http://stackoverflow.com/questions/13864510/exporting-stl-class-from-dll-why-is-there-no-warning-from-the-return-type), call it a workaround, not a redesign, which it isn't. Anyway, if you believe you should not be exporting string data members, then by the same token you should not be able to export a string getter. – n. m. could be an AI Aug 05 '15 at 16:15

2 Answers2

1

Having STL classes at the DLL interface is a very fragile design: if you do so, all the clients of the DLL must be built using the same version of the Visual C++ compiler, and the same settings (e.g. same _ITERATOR_DEBUG_LEVEL, etc.), and the same flavor of the CRT.
This is highly constraining.

You may want to follow different paths. For example, you could build a pure-C interface API layer wrapping your C++ classes, and export that from the DLL. DLL with a pure C interface are just fine (for example, think of the Win32 APIs).
Then you can distribute an header file in which this pure-C interface is wrapped in C++ classes, ready to be used by clients (for example, think of ATL/WTL, which are convenient C++ wrappers around pure-C Win32 APIs).

Or you could follow a COM-ish approach, exporting C++ interfaces.
You can find more details about that in this article:

HowTo: Export C++ classes from a DLL

Mr.C64
  • 41,637
  • 14
  • 86
  • 162
0

A DLL just provides references to global variables and functions, since you class uses neither, you cannot export anything from your class from the DLL. Typically you would put your class into a header file and share the header file between both your DLL and the harness executable just like you will do with a C++ project having multiple source files.

doron
  • 27,972
  • 12
  • 65
  • 103