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

Show parent comments

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