question regarding header files

S

siddharth_jain_1

Hello,

1) what do header files contain exactly?
Do they just contain the variable declarations, typedefs and
function prototypes?

2) taking the example of iostream.h: it contains the prototyoes of
various functions. which file has the actual code of these functions?


3) If I build my own header file (header.h) which includes some
function prototypes and I have another file (program.c) that has the
actual code for these functions, when I go to use those in another
program. If i #include "myheader.h" in my new file (project.c) will it
then know to look in code.c for the function?

4) Could someone please guide me to a good link from where I can read
about the usage of header files/


Thanks,

Siddharth Jain
 
V

Victor Bazarov

1) what do header files contain exactly?
Do they just contain the variable declarations, typedefs and
function prototypes?

They contain anything that you deem necessary to share between compilation
units. Headers are usually created to be included in more than one module
to be compiled.
2) taking the example of iostream.h:

BTW, there is no such header. I will assume we're talking <iostream>.
Also, you should know that _standard_ headers are not necessarily files.
Just so we're clear on that.
it contains the prototyoes of
various functions. which file has the actual code of these functions?

That's specific to the implementation. If the compiler vendor supplied
you with the source code to their implementation of the Standard library,
then the actual code is there somewhere. If the vendor didn't provide
the source code, then the actual code is on the vendor's computers and
you only get to see the declarations.
3) If I build my own header file (header.h) which includes some
function prototypes and I have another file (program.c) that has the
actual code for these functions, when I go to use those in another
program. If i #include "myheader.h" in my new file (project.c) will it
then know to look in code.c for the function?

No, it won't. You will need to compile 'code.c' separately and add it to
the command line of the linker so that the object code is linked into the
final program.
4) Could someone please guide me to a good link from where I can read
about the usage of header files/

www.google.com

V
 
E

eeykay

STLs usaully provides implementation also in the header files. Your
point 1. is correct. Regarding your point 3, I assume code.c includes
"myheader.h". So, the compiler compiles it to an code.obj file, which
the linker uses to link with your project.obj, to produce the final
program. You should look for how c++ program compiles to generate an
output for more in-depth details. Writing your own makefile will also
help understanding these things.
-EK
 
O

osmium

1) what do header files contain exactly?
Do they just contain the variable declarations, typedefs and
function prototypes?

They also usually contain the actual code (the function definition) for
templated functions.
 

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

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top