My product is a C++ library, which, on Windows, is distributed as a dll. It makes very little use of the c-runtime (basic iostream and that’s it), so I’m sure that all recent versions of the CRT will be fine.
Since my client is supposed to build his application using my dll, I don’t want to impose upon him any specific runtime version. I’d like my dll to bind to whatever runtime library version my client’s app is using (and I can assume that he’ll use dynamic linking for his CRT). After all, isn’t that what dynamic linking is all about? Is that possible?
EDIT: linking the dll against the static runtime libs won’t work either, because then the static runtime (from the dll) and the dynamic runtime (from the client’s application) will be mixed, which is bad.
EDIT: What I’m mainly asking is how do I tell the runtime loader to link my dll against whatever CRT the application is linked with? Something with the manifest, perhaps?
More generally, my question is how to build a nicely-behaving dll, that’s to be used by clients building they’re own applications?
EDIT: Thanks to the advice in the answers, I’ve transferred all references to std classes into inlined functions in my headers, and linked my dll with the static runtime libraries. It now seems to work even in applications linked with different CRT versions.
There’s no real way to ensure your DLL works with multiple runtimes — any of the types that change between them can lead to incompatibilities. For instance, the size of an object can change, or the location of members in them. There is very little room in C++ for this kind of thing.
The best thing you can do is statically link to the runtime and ensure the exported API is limited to types strictly under your control — no passing
std::stringto a function, no stdlib types as members, and don’tnewin one DLL anddeletein another. Don’t mix inline and exported functions (including constructors/destructors) for the same object, because member order and padding might change between compilers. The pimpl idiom might help here.