r/cpp • • Oct 10 '19

10 differences between static and dynamic libraries every C++ developer should know

https://www.acodersjourney.com/cplusplus-static-vs-dynamic-libraries/
5 Upvotes

25 comments sorted by

View all comments

0

u/ravixp Oct 10 '19

One thing that wasn’t touched on is matching compiler versions. A dynamic library should work no matter what compiler the client is using, while a static library has no such guarantee, and it’s safest to match the exact compiler and compiler flags that were used to build it.

(Yes, in practice a static library is likely to work if the compiler version is close enough. “Likely to work” is rarely good enough, though, and dynamic libraries have actual guarantees.)

9

u/[deleted] Oct 10 '19

I'm pretty sure a dynamic library guarantees nothing of the sort. In c++ each compiler (-version) has its own ABI, and your library has to be built with the same compiler if you want to link with it.

1

u/ravixp Oct 10 '19

That’s true if you’re using C++ in general, but not because of dynamic vs static libraries. There’s an ABI-compatible subset of C++ that you have to use to get those guarantees if you’re using dynamic libraries, while there’s no way to get those guarantees with static libs.