Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

basic header-file question

Status
Not open for further replies.

DoraC

Programmer
May 7, 2002
98
US
Hello,

I'm sure this question is quite basic, so please forgive me if it is!

I am writing a function (call it "f()") and some supporting data types (structures, unions etc.) that will be used in various other files. I know I shouldn't put the definition of f() in a header file, as header files are only supposed to contain declarations and prototypes. So, should I declare f() in, say, f.h and define it in f.c? And - most basic of all - programs that use f(), should they
Code:
    include "f.h"
and, if so, how will they obtain access to the definition?

On a similar note, I often #include stdio.h, math.h etc. in my programs. When I look in the directory that contains these files (on my computer, "Microsoft Visual Studio/VC98/Lib") I find the header file but - again - where are the definitions? Are they in some DLL that gets pulled in at runtime? Is the purpose of the header file ONLY to facilitate compilation?

:-S
thanks,
dora c
 
Well DoraC till an extent you are perfectly right. Header files facilitate in the compilation only. Actualy definitions of function bodies are required at the time of linking.

See you declare the function f() in say A.c . When you COMPILE it it will create say A.o which will have compiled code of A.c. Now if you have another file that wants to use this function f() in say B.c you can include the A.h(that contains the decalartion of f()). Now when you compile B.c it will create B.o. But if you want to create an executable, it will ask for all the missing function bodies like for f(). Body can be supplied from A.o which is a compiled version of f(). When you link them togetherIt will find all the missing function bodies and will successfully create an executable.

Same is the thing when you have stdio.h etc. On Unix their body lies in libraries lie libc.a etc. Compiler automatically links your code with these libraries to create your final executable.

Hope this helps.
 
To gasingh explanation and example , I want to add a particular example of using .h files.
The following is a valid header file :
//fields.h

FIELD (double, 15);
FIELD (float, 15);
FIELD (ptr,32);
FIELD (string, 1024);

And here is an example of how it could be included in the body of main() function:
// main.cpp

class Memblk
{
public:
Memblk()
{
};
void Store_double(){};
void Store_float(){};
void Store_address(){};
void Store_string(){};
void Store_ptr(){};


};
int main ()
{
Memblk *ptrMem = new Memblk();
#undef FIELD
#define FIELD(fieldname, size) if (ptrMem) ptrMem->Store_##fieldname();
#include "fields.h"
}


It will generate the call of the :
ptrMem->Store_double(),
ptrMem->Store_float(),
ptrMem->Store_ptr(),
ptrMem->Store_address().

-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top