3

When you compile a C source code does the compiler convert the whole header file to object code or just the functions of the header file you use?

Like for example, in the header file #include stdio.h, there are printf(); and scanf(); but even to use one of them, you have to include the header file "stdio.h", so when you compile a C source code which uses only printf(); but doesn't use scanf();, will the compiler also include the object code of scanf(); and other functions of "stdio.h" in the file?

2
  • 8
    Headers don't normally contain code. So you could include every header and it wouldn't make your object code bigger. I think you are asking about how linking and libraries work.
    – JS1
    Mar 5, 2015 at 5:25
  • 4
    "Like for example, in the header file #include stdio.h, there are printf(); and scanf();" Functions do not live "in" the header and you don't need to include the header to use them. All that's needed is a forward declaration. Headers just give you a convenient way of inserting a bunch of forward declarations (among other things) into a file.
    – Doval
    Mar 5, 2015 at 12:34

3 Answers 3

6

The compiler will generate code for everything.

The linker may (and usually does) not include code of unused functions in the final executable.

Implementation, of course, varies from linker to linker.

1
  • 1
    No, the compiler generates code (or data) for definitions, not for declarations. If the source file contains only declarations, the compilers will not generate any code, the object file will not contain any code or data. Some compilers will even issue a warning about this. The main difference between .h and .c files is exactly that: .h files, aka header files contain only declarations, including them does not generate any code par se.
    – chqrlie
    Mar 8, 2015 at 9:10
5

Indirectly, yes.

To know what happens to the code in the header file one must understand how the compiler handles them.

The #include directive

The #include directive is a preprocessor directive. Even before the compiler starts to analyze and translate the code it will process all #includes by inserting the whole code in the given header file to the position of the #include statement. (You can see what the preprocessor spits out when you use e.g. gcc -E ... on your source files.)

That is why include guards are important.

So the answer to your question is: Yes, the code in header files is translated to an object file - to be precise to the object file of the first translated source file which includes it in a translation unit.

The definitions in the header files have to be known in every translation unit which uses the definitions. If you translate your programm file by file and link it afterwards, you will have the code in the header file compiled multiple times. That is why you should put the implementation to a separate source file. If you do that you only will declare each function in every translation unit but not translate the implementation. All files will then be linked to the one object file with the implementation.

3

The compiler doesn't do anything with other object files. It merely compiles code to object files one translation unit at a time. It's the linker that takes your new object file, looks for symbols like "printf", and then "attaches" the implementation of printf from stdio.h's object file. In theory the linker could throw in the whole stdio.h object file, but in practice it should leave out all scanf code in the final executable since you don't need it.

So the literal answer to your question is: No, the compiler doesn't do anything like that. If you were using "compiler" to informally denote your entire toolchain, then the answer is: Yes at first, but only until it finishes checking which parts of the object file you do and don't need.

Note that this only describes static linking, where you combine all object files into a single executable before executing anything. Dynamic linking is a very different story.

2
  • Yes by compiler I meant the whole tool chain. Will it discard the object code which it doesn't find used in the program?
    – want2code
    Mar 6, 2015 at 7:00
  • @want2code Yes, I'm pretty sure any decent linker would discard unused object code (during static linking).
    – Ixrec
    Mar 6, 2015 at 7:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.