Thread: how do header filers work?

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    913

    how do header filers work?

    how do header filers work?

    i mean they have no source code but yet they still accomplish something, how?

    if i have a c file and a h file why cant the h file include the c file? what should i really do?

    why cant you just rename a .c file to .h?

    i havnt found anysite that explains why to any of these, just tha code in a header is wrong.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >i mean they have no source code but yet they still accomplish something, how?
    They accomplish nothing more than declarations. To use a name in C it must first be declared, a header file performs this task. However, declarations must have a definition before being used. That's where implementation files come in.

    A header file (*.h) is where the declarations reside, you use it to tell a program what names you intend to use and how. An implementation file (*.c) defines those declarations and is compile/linked to your program. For example, you have three files:
    Code:
    /* ctest.c */
    #include "test.h"
    
    int main(void)
    {
        f("Hello, world");
    
        return 0;
    }
    Code:
    /* test.h */
    void f(char *msg);
    Code:
    /* test.c */
    #include <stdio.h>
    
    void f(char *msg)
    {
        puts(msg);
    }
    Now you can compile test.c separately like so (assuming a Borland compiler for no reason):

    %>bcc32 -c test.c

    This compiles test.c and produces an .obj file. To compile ctest.c using test.h, you simply compile ctest.c and link test.obj:

    %>bcc32 ctest.c test.obj

    This separates the compilation and linking stages, but the advantages aren't obvious until you work on larger projects where recompilation of the entire program is very slow. Another alternative is to compile and link all in one shot:

    %>bcc32 test.c ctest.c

    So to answer your question, when required, header files have source code attached to them through implementation files which are linked in after compilation. The header file itself is there to make sure that your program knows what names are available during compilation (programs are compiled before they are linked, so you need the declarations). If the definitions do not exist, you get linker errors.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    so the entire source file is linked to a program? even if is never called or needed by a fuction?

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    28
    Greetings
    so the entire source file is linked to a program? even if is never called or needed by a fuction?
    Nope. Only the functions you actually use in the program are actually linked to the finall .exe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Replies: 4
    Last Post: 12-14-2005, 02:21 PM
  3. header and source files
    By gtriarhos in forum C Programming
    Replies: 3
    Last Post: 10-02-2005, 03:16 AM
  4. Header file (multiple inclusions)
    By cjschw in forum C++ Programming
    Replies: 1
    Last Post: 08-10-2004, 10:28 AM