__declspec / DLL / C++ confusion
I just came across this article. If I'm reading it right, the author seems to suggest that one cannot export C++ classes (either in whole or in part) from a DLL (i.e. using the __declspec modifier). Instead, you should use a C style function to create an instance of the class and export that instead. However, a quick search of this forum reveals loads of posts which suggest the opposite. Who's right and who's wrong?
Re: __declspec / DLL / C++ confusion
You can not export a class, but you can export the methods and variables individually, you can also export these with C++ way of exporting. You have Visual C++ project types like MFC DLLs that export classes.
The mechanism of exporting a method with C style which returns an instance of given class is a very much tradditional way of doing it.
Re: __declspec / DLL / C++ confusion
Quote:
Originally Posted by
John E
I just came across
this article.
If I'm reading it right, the author seems to suggest that
one cannot export C++ classes (either in whole or in part) from a DLL (i.e. using the __declspec modifier). Instead, you should use a C style function to create an instance of the class and export that instead. However, a quick search of this forum reveals loads of posts which suggest the opposite. Who's right and who's wrong?
You read it wrong. This is the quote:
Quote:
You can not directly export a C++ class from a dynamically loadable DLL.
The main point author makes is about plug-ins, i.e. a situation when dynamic dll loading [Edit: and explicit linkage!] is unconditionally implied.
Indeed, you just cannot explain your compiler that you intend to link your classes constructors/destructors/members later at runtime, somehow, maybe... :) That's why C-style export is mandatory for plug-ins. Besides, plug-ins are most of the times about cross-language usage, which is another reason for C-style export.
But in case you never need your dll to be loaded dynamically, you surely may export/import classes with no problem (well, problem might happen, but in this case it's gonna be very specific :))
Re: __declspec / DLL / C++ confusion
Sorry Igor, you've confused me now. ALL dll's are dynamically loadable aren't they? Or are you referring to DLL functions loaded using LoadLibrary/GetProcAddress etc?
Re: __declspec / DLL / C++ confusion
Yes, and thank you for noticing that. :) Been talking about 'dynamic loading' I meant (and I believe, the author did as well) explicit linkage by means of GetProcAddress. Maybe that was the main point of your confusion from the very beginning.
Re: __declspec / DLL / C++ confusion
Yes, I think it was, now you've pointed it out..! :)
Re: __declspec / DLL / C++ confusion
Another question along the same lines....
With a standard 'C' source file, a .DEF file can be used to export functions if the program was originally written without __declspec modifiers. Can the same be done for C++ (i.e. class member) functions? I've never seen it done but it'd be interesting to know whether or not it's possible. At the very least, I assume that name mangling is going to make this tricky :confused:
Re: __declspec / DLL / C++ confusion
Quote:
Originally Posted by
John E
Another question along the same lines....
With a standard 'C' source file, a .DEF file can be used to export functions if the program was originally written without __declspec modifiers. Can the same be done for C++ (i.e. class member) functions? I've never seen it done but it'd be interesting to know whether or not it's possible. At the very least, I assume that name mangling is going to make this tricky :confused:
The DEF applies to functions only. With regard to classes, it's almost not worth exporting a class unless you [re]compile the dll each time you compile the app. The name mangling must match exactly and can change from compiler version to version.
1 Attachment(s)
Re: __declspec / DLL / C++ confusion
Quote:
Originally Posted by
John E
Another question along the same lines....
With a standard 'C' source file, a .DEF file can be used to export functions if the program was originally written without __declspec modifiers. Can the same be done for C++ (i.e. class member) functions? I've never seen it done but it'd be interesting to know whether or not it's possible. At the very least, I assume that name mangling is going to make this tricky :confused:
Anyway, it's possible. Though not without certain limitations... ;)