Header files in C and its use

Library header files only contain function declarations. So how exactly does the function work when we call the library functions?

Example: int strcmp ( const char * str1, const char * str2 );

is the declaration for the strcmp function in C.

When we call the strcmp function in a program, how exactly does the strcmp function execute if the function body is not present in the header files?

+3


source to share


6 answers


The code you want to use as printf

is already compiled and stored somewhere. Your final executable is created when the linker links your object code to the standard library.

try it gcc -c somefile.c

and see what you get.

Also try the parameter gcc -v

and examine the output it gives.

Find out what the team is doing ld

and you will get an answer.

And this is what I think should help you: Everything about compilation / linking / related

UPDATE:



Also imagine if you have defined printf

for each program in stdio.h

, and you have includedstdio.h

program.c :

#include <stdio.h>  //suppose this contains definition of printf     

int main()
{            

/*Call printf()  somewhere or maybe not */

}

      

gcc program.c -o output

So, all your definitions that are in stdio.h

will be replaced in the file program.c

and your executable output

will become pretty HUGE . Instead, it would be better to name functions that you use frequently, for example printf

in stdio.h

, from one place when they are needed

+2


source


Header files

contain only function declarations. This is one of their main goals, to expose the content you should be working with without worrying about implementation.

As far as the implementation location is concerned, at creation a runtime library is added to your program, so as a coder you can call all of these functions since you have a "forward" declaration of their function definitions, but that is not until loaded implementation.



The reason for this is that it would be foolish to copy implementation code like printf into every single program that uses it. That would be a waste of memory! So instead, you are only given declarations, the programmer and the OS share the implementation of the function in memory at runtime.

+3


source


To understand how the strcmp program can be used by the program even hard, the compiler only sees its expression, you need to understand the linking process. Compiling a C program has three steps:

  • Preprocessing (resetting all included headers to the source)
  • Compilation (creating an object file from source)
  • Linking (link multiple object files together)

In the third step, the so-called linker (usually ld

) basically dumps the contents of all linked object files to the executable and resolves all symbols. A symbol is anything that has a global name, such as a function or a global variable.

When your program calls strcmp

, the object file generated by your compiler does not actually contain the definition of the specified function. Most likely there is a reference to a symbol strcmp

that will later be resolved by the linker. The compiler looks at all object files as well as global libraries and then resolves those references.

+3


source


strcmp

is in the runtime library that is added to your program when the executable is created. How this works varies by operating system.

+2


source


In this case, it is no different from any other custom header.

If you don't include the appropriate header (for example, string.h

for strcmp

), then the implicit declaration is done by the compiler.

If the function definition is not found (i.e. not in the library as per your assumption) at runtime, then the linker will complain when linking to the standard C library, which is executed by default (for example, at runtime -lc

in the case of gcc).

+2


source


It is present in the compiled library. When you compile your project when linking with any library, the precompiled library is added to the compilation.

+1


source







All Articles