简体   繁体   中英

Using C++ standard library in dll internally

I have read in many places that you should never use C++ standard library classes in the api of a shared library. Now what I am unsure about is whether it is safe to use a standard library class like std::vector in a dll privately, and only expose a pointer to the data.

I am trying to create a dll that will dynamically be loaded at runtime, does this change anything?

Also do I need to worry about how I will link the standard library to the dll? Is that consistent across platforms?

Yes, it is generally considered bad practice to use non-POD data types across the DLL boundary. Bad things happen when the calling module uses different versions/implementations of the data types that the DLL's public API uses (ie, when the calling module uses a different version of the same compiler/framework that the DLL uses). Or, even when the calling module doesn't support the data types at all (ie, when the calling module is written in a completely different compiler/language than the DLL is written in).

When sharing DLLs, you should design the public API as though it were targeting the C language only, as that is the most portable option amongst various compilers/languages that support accessing DLLs.

However, the DLL can freely use whatever it wants internally for its private implementations. Just don't expose those details publicly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM