Linker questions

Can someone help me with a couple of general questions about the linker?

My understanding is that when I compile some code the linker will examine the code and spot if a function is used or not and then not bother compiling it if nothing uses it? So if I write functionA() but never actually use it, then it won't get compiled - is this correct?

What I want to know is how thoroughly the linker checks. Say I have functions a(), b() and c(), and these are used exclusively by functions one(), two() and three() which are all used exclusively by z() only. If I don't actually use z() is the linker smart enough to know that it doesn't need to bother including any of the other functions?

Also if a function is only called by a impossible circumstance (for example if(1==2) ) is the linker smart enough to spot that the function will never be called, and therefore won't be compiled?

Finally, is there anyway I can check what has / hasn't been included? are any intermediate files created that I can check to see what functions have actually been included?

Thanks

My understanding is that when I compile some code the linker will examine the code and spot if a function is used or not and then not bother compiling it if nothing uses it?

Wrong. The compiler compiles ALL the code. It generates an object file. The linker then examines all the object files it is told to look in, to find the code it needs. There may be tons of object files that supply nothing, and there may be object files that have tons of functions defined that do not get used. The linker goes shopping, and only puts stuff in its cart that is on its list.

So if I write functionA() but never actually use it, then it won't get compiled - is this correct?

No.

What I want to know is how thoroughly the linker checks. Say I have functions a(), b() and c(), and these are used exclusively by functions one(), two() and three() which are all used exclusively by z() only. If I don't actually use z() is the linker smart enough to know that it doesn't need to bother including any of the other functions?

You've got your premise backwards. The linker may see that it needs to include z(), and that means that it needs to include one(), two(), and three(), and that means that it needs to include a(), b(), and c(). If the linker does not need to include z(), then the list of functions that z() depends on don't even enter the picture.

Also if a function is only called by a impossible circumstance (for example if(1==2) ) is the linker smart enough to spot that the function will never be called, and therefore won't be compiled?

The linker is not involved in that decision. The compiler is, and it is smart enough to see that some code simply can never be executed. So, its off to the bit bucket with that code.

Finally, is there anyway I can check what has / hasn't been included?

Yes, but there is probably not going to be a straight-forward mapping between the various files that is easy for you to follow.

And that process can not be triggered by the IDE.

Thanks Paul,

So I think I'm right in saying that un-used code won't end up in the final project? (even if I'm getting the terminology wrong)

Can you point me in the direction of an easy-to-read tutorial on this sort of stuff?

thanks

So I think I'm right in saying that un-used code won't end up in the final project?

You are still thinking of the process as a matter of excluding unused code. It is not. It is a matter of only including what is needed.

Think about going to the store, with a grocery list. You don't push the cart around tossing random stuff in, then, when done, say "Lets see whats in here that I don't need". You only put what is needed in.

If the list says "stuff to make tacos", you expand the list to include meat, seasonings, taco shells, salsa, cheese, lettuce, tomatoes, and olives. But, you don't decide that ice cream and tampons belong in the cart just because they are nearby on the shelves.

The linker has a list. If a function is on the list, it gets added to the cart and removed from the list. If the function has a list (of dependencies), its list is merged with the linkers list. When the list is empty, the linker is done.

Can you point me in the direction of an easy-to-read tutorial on this sort of stuff?

No. I doubt that there is any such thing.