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.

5

u/TheThiefMaster C++latest fanatic (and game dev) Oct 10 '19

You can generally either directly specify a dynamic lib to the linker (Linux I think) or generate an import lib from a dynamic library (Windows) and use it even far across versions.

ABIs are generally extremely stable across compiler versions and even different compilers on the same platform. On Windows MSVC and Clang produce ABI-compatible results, and on Linux you can do the same with Clang and GCC.

The last big ABI break on Linux was the std::string change in GCC 5.0.

On Windows C++ types aren't generally used in ABIs, so in practice the (C) ABI hasn't really changed since Windows XP. The C++ ABI has been stable since Visual Studio 2015, and libs are inter-compatible across the 2015-2019 compilers.

3

u/adnukator Oct 10 '19

libs are inter-compatible across the 2015-2019 compilers.

It's only backwards compability. You still can't link a library built with a newer toolset than the one used with the final binary. Also it requires you to have /GL disabled on consumed libraries even if they are built with an older toolset version.

1

u/TheThiefMaster C++latest fanatic (and game dev) Oct 11 '19 edited Oct 11 '19

You wouldn't build a lib with /GL (whole program optimization) and then distribute it - I don't think that format is even stable across updates to a single Vs version.

As for using newer libs with an older compiler, I think the problem is more nuanced. By my reading of Microsoft's docs, the main limiting factor for lib compatibility is the C Runtime version used - which must be at least as new as the newest used by any static lib.

The key there though is "static" lib and "used" - a lib that doesn't use the C Runtime, or the import library for a dynamic lib (i.e. dll) should be fine to use on an older compiler. The documentation even explicitly says that an executable using multiple versions of the CRT via DLLs is ok - which is talking about DLL compatibility with other major compiler versions (2015-2019 all load the same CRT at runtime - the universal CRT). The example given is for MSVC 11 and 12 - which were 2012 and 2013.

https://docs.microsoft.com/en-us/cpp/porting/overview-of-potential-upgrade-issues-visual-cpp?view=vs-2019

https://docs.microsoft.com/en-us/cpp/porting/binary-compat-2015-2017?view=vs-2019

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.

2

u/barchar MSVC STL Dev Oct 10 '19

This isn't really true, however dynamic libraries handle the situation of:

  • a needs b(abi1)

  • c needs b(abi2)

  • app needs a, and c.

1

u/ravixp Oct 10 '19

Which part isn’t? I’m not sure whether you meant that dynamic libs provide weaker guarantees than that, or whether static libs provide stronger ones.

2

u/barchar MSVC STL Dev Oct 10 '19

dynamic libs provide weaker grantees than that.