Thread: Makefiles
View Single Post
Old 21st October 2008
ocicat ocicat is offline
Administrator
 
Join Date: Apr 2008
Posts: 3,318
Default

Quote:
Originally Posted by JMJ_coder View Post
If the #include isn't necessary for the standard library, why do we #include them?
Unfortunately, gcc(1) hides the call to the linker, but you need to realize that there are two phases which generating binary:
  • The compiler itself only translates source code into object files. Nothing more.

    #include statements are required by the compiler for type-checking reasons. The compiler wants to know a priori whether a symbol it encounters represents a variable, struct, class, function, etc. If the compiler cannot deduce what a symbol represents, it flags the symbol as undeclared.
  • All object files are then passed to the linker. Note that the only library the linker will consult when creating the resulting binary is libc by default. Any other library which will be required must be explicitly mentioned on the command-line.
Historically, some C compilers prior to the ANSI standard didn't require a #include statement for functions found in libc, but the stronger type-checking required by the ANSI standard forced explicit inclusion of all necessary headers.

Of course, C++ implements even more stringent type-checking than C. Type-checking is deemed to be important because it catches an entire class of subtle bugs, & error messages can be more precise in pointing out the root cause.

What you should be taking away from this discussion is that errors can occur at different points in the process, & these errors manifest themselves in different ways.
  • #include statements are preprocessor directives needed to allow compilation to complete. Nothing more.
  • The linker has no knowledge of what happened during compilation nor does it need to care. The linker simply takes all explicitly supplied object files + all explicitly supplied libraries + libc & attempts to splice together the resulting binary. Nothing more.
There is a reason why it is all referred to as a toolchain...

Last edited by ocicat; 21st October 2008 at 04:32 PM.
Reply With Quote