2

When you include a header

#include "foo.h"

you also need a source file, foo.c, with the definitions of the prototypes in the header that you pass to the compiler when compiling: gcc main.c foo.c. Why doesn't the same have to happen for headers from the standard library? For example, say you have a main.c file with

#include <stdio.h>

when you compile, you don't have to then write gcc main.c stdio.c, you just write gcc main.c.

Why is this? Does the compiler/linker know where to look for the source files when you compile and just automatically adds them? If not, then how would the compiler know what to do with the function prototypes?

Kalcifer
  • 1,211
  • 12
  • 18
  • 1
    Depending on the compiler a C standard library (or part of it) are linked automatically. And yes, the linker normally knows where the standard libraries are stored. – Gerhardh Aug 10 '21 at 07:12
  • 3
    "libraries" include both the headers and the binaries with the library implementation. You *include* the headers and *link* the libraries. The standard C lib is linked in automatically. Other libs are linked in via `-llibname`. – kaylum Aug 10 '21 at 07:13
  • 1
    BTW: For various C standard functions you still need to manually tell the linker to include math library – Gerhardh Aug 10 '21 at 07:13
  • 1
    You can link manually if you add `-nostdlib` in GCC and Clang. But otherwise, compilers will generally link the standard library for you. – mediocrevegetable1 Aug 10 '21 at 07:18
  • Attention: Not all generic linker (or generic compiler that includes/automanages linker) know where the standard library are. The same for the headers (include) files. The compilers/linkers may use some "tricks" and they may know some standard paths where libraries and header-files are. Old C compilers such TC, QC need setting to individuate livraries and headers. In our days, in most cases, installing the compiler creates the environment necessary for code compilation to be successful in standard cases. – Sir Jo Black Aug 10 '21 at 07:24
  • Typically the source code isn't present on your system. It's already compiled. So your compiler never see anything like "stdio.c". It's your linker that takes the pre-compiled functions and links them into your program. And yes, compilers typically knows where to look for them. – Support Ukraine Aug 10 '21 at 07:30

2 Answers2

2

When you have a header file called foo.h it doesn't necessarily mean that the there should be a source file associated with it called the foo.c. There can be be independent source and header files.

When you do compile a simple C code, hello_world.c

#include <stdio.h>
int main() {
   // printf() displays the string inside quotation
   printf("Hello, World!");
   return 0;
}

And when you compile the code, the code is first preprocessed as part of this the header file inclusion happens and any header file that is to be included and is present within the </> (#include <stdio.h> in our case) is searched for it's definition in the current directory and a few predefined search path already know to the compiler (the preprocessor). Few of the standard header file definitions are present such as /usr/include, /usr/local/include, etc. more info

The second part of your question being as to how does the compiler know the definition of say for example printf() when we don't associate a source file at compile time that is cause when you compile any program the compilers by default links it with a std C library called the libc which contains the definition of functions such as scanf,printf,etc

0

When you include a header /* ... */ you also need a source file

No, you do not have to.

  1. Your header file may contain only macro and data types declaration. No source file needed
  2. The code was compiled before and you have only library or object files. You need to let know the linker that it has to use those files.
  3. You use standard library files. generic linker needs to know the library locations. Many compilers pass this information on their own. for example gcc uses spec files where you may specify where your libraries are (and many more things).
0___________
  • 60,014
  • 4
  • 34
  • 74