I've been trying to figure out how linkers work but I can't find any good information on the topic. Does anyone have any good in depth info on the subject?
I've been trying to figure out how linkers work but I can't find any good information on the topic. Does anyone have any good in depth info on the subject?
Linkers basically work by taking each of the object code files that the compiler has produced, identifying all of the variables, classes, methods, etc. that haven't been defined in that file, and looking in all of the other object code files and libraries you have listed as linker inputs for definitions that match. If everything the linker needs gets found, then it produces an executable file. If it doesn't then you get a list of "unresolved reference" items.
Different linkers work in different ways. In Microsoft's Visual C++, for example, you can list object code and library files in pretty much any order and the linker will figure it out. In other C++ toolchains (gcc for one, I think), you have to list files in order of dependence. For example, if library A depends on a function in library B, then you have to list A first and then B because the linker only brings in object code to satisfy the reference it knows about so far. If you list B then A since the linker hasn't seen A yet, it doesn't know about its dependence on B and won't bring those parts in.
Think about making an apple pie. You need a pie crust and apple filling. The pie crust needs butter, flour, salt, ice water, and maybe a little sugar. The apple filling needs apples, more sugar, a little more flour, cinnamon, nutmeg, and maybe a few other spices. You, the baker, must link all of that together to make an apple pie. You know that flour and sugar are found in the pantry library, butter is in the refrigerator library, spices in the spice library, ice in the freezer library, and water in the sink library. Ice water depends on both ice and water. So you have to link all of those dependencies together in the right order to get your pie. If you are only allowed to visit each library once (as in gcc) then if you go to the pantry and pull out only sugar and flour for the crust and then put them back, you're stuck because when you want to make the filling you find you are missing sugar and flour, but the pantry library has already been processed and is gone. So you have to make sure you list both the crust and the filling before you list the pantry library. If you are a lucky Microsoft baker, all of the doors are still open and you can just reach in and grab the sugar again.
If you google for "compilers and linkers" there are lots of good articles. If you google for "apple pie recipe" you'll find lots of them too.
<=== The Great Pumpkin says ===>
Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.
Atomic_Sheep (15th December 2020)
Bookmarks