C++: Compiler and Linker functionality

后端 未结 8 449
野趣味
野趣味 2020-12-10 08:39

I want to understand exactly which part of a program compiler looks at and which the linker looks at. So I wrote the following code:

#include          


        
相关标签:
8条回答
  • 2020-12-10 08:54

    Say you want to eat some soup, so you go to a restaurant.

    You search the menu for soup. If you don't find it in the menu, you leave the restaurant. (kind of like a compiler complaining it couldn't find the function) If you find it, what do you do?

    You call the waiter to go get you some soup. However, just because it's in the menu, doesn't mean that they also have it in the kitchen. Could be an outdated menu, it could be that someone forgot to tell the chef that he's supposed to make soup. So again, you leave. (like an error from the linker that it couldn't find the symbol)

    0 讨论(0)
  • 2020-12-10 08:59

    The missing semi-colon is a syntax error and therefore the code should not compile. This might happen even in a template implementation. Essentially, there is a parsing stage and whilst it is obvious to a human how to "fix and recover" a compiler doesn't have to do that. It can't just "imagine the semi-colon is there because that's what you meant" and continue.

    A linker looks for function definitions to call where they are required. It isn't required here so there is no complaint. There is no error in this file as such, as even if it were required, it might not be implemented in this particular compilation unit. The linker is responsible for collecting together different compilation units, i.e. "linking" them.

    0 讨论(0)
提交回复
热议问题