How do header files work?

V

vijay

I am trying to learn C and am struck at header file stuff. I have the
following source files in my directory:

header.h
------------
int a_function(int);

library.c
-----------
#include "header.h"

int a_function(int x) {
return x + 1;
}

program.c
--------------
#include "header.h"
#include <stdio.h>

main() {
printf("%d", a_function(4));
}

Now when I try
gcc -o program program.c, I get

/tmp/cciTe17G.o(.text+0x19): In function `main':
: undefined reference to `a_function'
collect2: ld returned 1 exit status

In fact, I had not expected it to work but I really don't know what I
need to do fix this.
 
S

santosh

vijay said:
I am trying to learn C and am struck at header file stuff. I have the
following source files in my directory:

header.h
------------
int a_function(int);

library.c
-----------
#include "header.h"

int a_function(int x) {
return x + 1;
}

program.c
--------------
#include "header.h"
#include <stdio.h>

main() {
printf("%d", a_function(4));
}

Now when I try
gcc -o program program.c, I get

/tmp/cciTe17G.o(.text+0x19): In function `main':
: undefined reference to `a_function'
collect2: ld returned 1 exit status

In fact, I had not expected it to work but I really don't know what I
need to do fix this.

You have not supplied the definition for a_function. Including header.h
in program.c merely supplies a_function's declaration. It's definition
is in library.c which needs to be compiled along with program.c or
separately compiled and the resultant object code of library.c and
program.c linked together. The error you are getting is from the linker
which cannot resolve the call to a_function in the object file produced
from program.c.

Add the library.c file to the compiler invocation command, or the object
file produced from compiling it. The exact details will vary from
compiler to compiler. For gcc do:

gcc -o program program.c library.c

or

gcc -c program.c
gcc -c library.c
gcc -o program program.o library.o

Under Windows the object files might have .obj as extension.
 
W

Walter Roberson

I am trying to learn C and am struck at header file stuff. I have the
following source files in my directory:

Now when I try
gcc -o program program.c, I get
/tmp/cciTe17G.o(.text+0x19): In function `main':
: undefined reference to `a_function'

You have two source files, library.c and program.c, but your gcc command
only compiles and links one of those, program.c .

When you use a header file, it does NOT cause anything extra
to be automatically linked in.
 
B

Bill Reid

I am trying to learn C and am struck at header file stuff.

From the name "header file", you might guess it is simply
all the stuff you'd have to type at the top ("head") of the file to
declare some constants, variables, and functions that have
been declared and defined in "external" files (every file BUT
the one your working on).

In your example, the really important thing that you are looking
for is the "extern" linkage keyword (which nobody has mentioned
yet). This keyword tells the compiler that the function or variable
you are declaring at the "head" of your file (actually in the "header
file) is actually compiled in a separate object file and/or library,
setting up the object file you compile to "link" to the "external"
object file.
I have the
following source files in my directory:

header.h

This should be:

extern int a_function(int);
library.c
-----------
#include "header.h"

int a_function(int x) {
return x + 1;
}

This is fine, though in this simple example you actually don't even
need:

#include "header.h"

That's because the compiler for your library.c file doesn't need
(or even want) a declaration for your a_function(), or anything else
for that matter (you don't need to "include" ANY header file).
The header.h file in this case is ONLY for OTHER source code
files.
program.c
--------------
#include "header.h"
#include <stdio.h>

main() {
printf("%d", a_function(4));
}

Now when I try
gcc -o program program.c, I get

/tmp/cciTe17G.o(.text+0x19): In function `main':
: undefined reference to `a_function'
collect2: ld returned 1 exit status

As noted by others, you need to compile BOTH source code files
and then "link" them together using a linker program to product the
target "program" executable program.
In fact, I had not expected it to work but I really don't know what I
need to do fix this.

The exact procedure to do this depends on your compiler and
development package. In just all all packages I can conceive
of, you have a linker program, and some facility for controlling
the compilation process to produce the final program from
the multiple source/object/library files that is is composed of...
 
D

David Thompson

This should be:

extern int a_function(int);
It could be, but function declarations always default to external
linkage and a style that assumes this isn't inherently bad.
This is fine, though in this simple example you actually don't even
need:

#include "header.h"

That's because the compiler for your library.c file doesn't need
(or even want) a declaration for your a_function(), or anything else
for that matter (you don't need to "include" ANY header file).
The header.h file in this case is ONLY for OTHER source code
files.
It isn't needed, but it can be 'wanted'. Assuming you use prototypes
(as the OP did) #include'ing the header in the implementation provides
a compatibility check between the declaration(s) in the header (used
by the other translation units) and the implementation(s), and catches
errors the linker typically can't and doesn't (and certainly isn't
required to). For this check to be done, you need to recompile
implementation(s) as well as client(s) whenever the interface
(potentially) changes; many IDEs or other build managers like make
will do this automatically if the header is #include'd as above.

- formerly david.thompson1 || achar(64) || worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top