4

At a high level, I understand we use #include statements to make code from other files available to the current file. But I don't understand why we include a header file, which contains declarations but no definitions.

Maybe I need to learn more about the compilation/linking process to fully understand the mechanics, but is there a high level concept I'm failing to grasp at the outset?

Edit: All the answers helped clarify my question, which boils down to: once we've notified the compiler that a function is defined elsewhere, how does it figure out where to find that definition?

1
  • 1
    The answer to your 'edit' question is: the compiler doesn't figure it out; you do. You invoke the compiler with options and files such that the linker (which is run by the compiler to convert object files and libraries into an executable) has all the information (functions, etc) that it needs. The standard C library works this way. Functions such as printf() are declared in <stdio.h> and the compiler links the C library automatically so that the definition of printf() is found. For your code, you either list the object files on the command line, or put them in a library and link that. Commented Mar 21, 2015 at 6:10

5 Answers 5

7

Because if you include the header where there are definitions, in different .c files, you will have Multiple Definitions.

The declaration is sufficient, because it allows the compiler to generate the calling code, after that the linker takes care of finding the definition and links the function call to the actual definition.

Sign up to request clarification or add additional context in comments.

3 Comments

Maybe the OP wants to know the why this leads to multiple definitions and why this is a bad thing. For example; the compiler processes the files from top to bottom, per file and the linker uses that information to gather the required code pieces. When a piece of code is defined in multiple compiler outputs it get confused which definition to use :)
Right. What I'm confused about though, is how declaring the interface is sufficient to gain access to the definition at all.
@ivan As iharob says in this answer with declaration in the header and including it in .c file that compiler gets to know that there is function whose definition exits at some place
2

One reason is you can use pre-compiled libraries as well.

Comments

1

why we include a header file, which contains declarations but no definitions.

Let's say we have a file

header.h

extern void func(); 

And this function is defined in some file f1.c and will be called by f2.c then include the required header to notify the compiler that the function definition exists in some other file without including the header the compiler will not know what the call to func() is within main()

f1.c

void func() { } 

f2.c

#include "header.h" int main() { func(); } 

There can be multiple declarations but there can't be multiple definitions for the same function

2 Comments

I guess what I'm wondering, then, is how does the compiler know where to find the definition?
@ivan If we consider this case where you need a function in some other header file and you have a call for it inside your .c file, including header will say that the function defintion exists in some other file else you will have compier error
0

Some people developing libraries do not want to let everyone know full code. They make it a library and then create the headers so everyone could use it. Moreover, @iharob's right, if we include definitions in headers, it's gonna make lots of troubles with code.

1 Comment

I hadn't thought of that. So you can use an included function essentially like a black-box. Cool
0

The header files with a .h extension only includes the declarations of the functions. Its actual implementation is mentioned in a separate .c file. It is the .h file that further includes the .c file which is present in the .h file's local directory . Things will be even clearer if you try to do a project on compilers where you create your own header files

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.