ARTICLE AD BOX
After a couple of years in the industry, I am only now facing the dilemmas of C++ linkers.
In the good old days of university, I had this simplistic view where "static library" meant the library was self-contained and did not need any external code to run its own functions; whereas a "dynamic library" (or, should I better say, a dynamically linked library) was a library that needed to load some other code at runtime. In my mental model, dynamic libraries would have a list of symbols they need and symbols they provide, and the OS would take care of locating those symbols and providing them at runtime.
I am now trying to wrap my head around the linkage problem at large. Could you recommend any sufficiently in-depth, reputable sources about the process of linkage in c++? As I said, I know the basics, but right now I need beginner-intermediate material to find out about the details in a way that is not overwhelmingly nitpicky. Of course, if someone wants to put the effort and write an answer here, that's more than welcome, but I assume much has been published in this regard, and it's simply not coming up in my internet searches.
Some of the questions I'd like to answer after reading the material are:
what happens when you link a static library to another static library? Do we get a "fat" binary that contains the first library as well? Can I chain static and dynamic at will? What could break? I know MSBuild provides a the /MT /MD and /LLD switches, that control whether the C runtime is linked statically or dynamically, but this seems to be orthogonal to whether I am building a .lib (static) or a .dll (dynamic) library, right? Are there significant differences between MSBuild and ld when it comes to this kind of matter? Raymond Chen has a few good writeups about the "classical model" of linking: does that still apply as of today? One widespread pattern about dynamic libraries is -at least in my company- the explicit import/export, in which functions marked for export are labeled as extern "C" and their signatures are updated to only use POD. The function pointers will then be cast appropriately by the consumer after a dlsym() or analogous call. Is there a better way? Is there the option I could link against a shared library and simply have the linker "know" what symbols the library provides?Thank you in advance!!
