I was having a discussion with a colleague today. He claimed that writing a DLL in C would allow for any other application, written in whatever language, to use that DLL. BUT, if that DLL is written in C++, the number of applications that could use that DLL is limited (maybe because of language constraints).
I hope this question isn't a Gorilla vs. Shark kinda question. If it is, please close it.
Most languages provide an (easy) way to call C function from a DLL. It is not the case with C++, since C++ ABI (the binary interface of C++ function) is vendor-specific.
Added to that, it will be nearly impossible to interface with C++ DLL that use advanced C++ constructs like templating or the STL.
However, the contents of your DLL can be written in C++, you only need to make sure that your interface is C-compliant. To do that, do not use C++ constructs in you interface and surround your declarations with :
#ifdef __cpluscplus
extern "C" {
#endif
/* You declarations here */
#ifdef __cpluscplus
}
#endif
... this way, you wrap your C++ library with a C interface.
EDIT : As Mats Petersson wrote, do not forget to ensure that you handle every possible C++ exception in your wrapper.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With