Thread: Header Help

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    7

    Header Help

    Sorry if my question is confusing, but I'm kinda lost on this whole header thing.

    If header files only hold function prototypes, then do the actual function definitions go into another .c file (is that what an implementation file is?)? Now when you're compiling a program, how does the compiler know where the function definitions are if you're only including the " #include header.h " in the program file? I heard something about linking, but I don't know anything else.

    Also, if anybody had the actual commands for the GCC compiler in UNIX, that would be great. thanks in advance.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: Header Help

    If header files only hold function prototypes, then do the actual function definitions go into another .c file?
    The declarations should be in the header (.h) file and the definitions (the actual code) should be in the code files (.c/.cpp). Actually, you can place the declarations directly in the c file since #include simply pastes the text in the h-file into the c-file, but since you might want to have the same declarations in several c files, you better place it in a separate header file. It's also a better coding if you separate them. Someone may be reading your code and just want to know what functions you have, not how they are buildt.

    Now when you're compiling a program, how does the compiler know where the function definitions are if you're only including the " #include header.h " in the program file? I heard something about linking, but I don't know anything else.
    When compiling, the compiler creates object files (.obj) from your c files. Then the linker puts them together into an executable file (.exe), ready to be run.

    I don't exactly get your question. If you wonder how a function in one module can know what functions there are in another module, then that's what header files are for. To tell what functions are available.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >then do the actual function definitions go into another .c file
    Yes.

    >is that what an implementation file is?
    If that's what you want to call it, yes.

    >how does the compiler know where the function definitions are if you're only including the " #include header.h " in the program file?
    You either compile the implementation file separately and link it with the driver, or compile everything together and the compiler will sort it out.

    >Also, if anybody had the actual commands for the GCC compiler in UNIX, that would be great.
    Everything at once (a.c, b.c, b.h):
    $ gcc a.c b.c
    $ a.out

    Separate compilation (same files):
    $ gcc -c a.c
    $ gcc -c b.c
    $ gcc a.o b.o
    $ a.out

    Or you can get more information here.

    -Prelude
    My best code is written with the delete key.

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. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM