Can understand difference between header files and libraries.

This is a discussion on Can understand difference between header files and libraries. within the C Programming forums, part of the General Programming Boards category; Hi.I cant understand what's the difference between header files and libraries....all i know is that header files are excuted in ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    46

    Can't understand difference between header files and libraries.

    Hi.I cant understand what's the difference between header files and libraries....all i know is that header files are excuted in the preprocessor phase while the libraries are excuted in the linking phase.Thanks in advance
    Last edited by KidMan; 07-06-2006 at 08:57 AM.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    A library contains already-compiled-code. For example if you want to use function foo() in two or more projects, you have a couple options:

    (1) you could simply copy the code for function foo() into every project that needs it. But that presents many problems, especially if the projects are not your own. If I want to use your function foo() you would have to give me the source code so that I can add it to my project. A month or so from now you might want to change the function to add new features, now you have to provide everybody with new source code.

    (2)Compile function foo() by itself and put it into a library along with other functions that you want to use in two or more projects or you want other developers to use. Now if you change the source code you just have to recompile, rebuild the library, and send the library to other developers. that makes life easier for other developers, and helps you too because now you are not giving away priporitory information (such as company secrets). You may NOT want to share the code with anyone else, so you put precompiled code in a library.


    Header files: normally they contain only definitions and function prototypes. They do not usually contain executable code (inline functions and c++ templates are two common exceptions). If you create a library you will also want to create a header file that describes the functions in the library.
    Last edited by Ancient Dragon; 07-06-2006 at 09:26 AM.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    All of the standard functions you use in C are in libraries. Let's say the printf() function is in the library called libc that came with your compiler. That libc library contains the precompiled code for the printf() function.

    Now the trick is to tell your compiler how the printf() function is defined so you can call it from external files (e.g. your program). That's where the header file comes in. When you #include <stdio.h>, that header file contains printf()'s function prototype so when you use printf() in your program the compiler knows what printf()'s return type and what kind of/how many arguments it accepts.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    During the preprocessor phase, the compiler takes the contents of the header files and basically does a cut and paste job of those contents into the source file creating an intermediate file which is the one the compiler then parses through to generate object code. For instance, lets say you have a source file and a header file:

    Header (my_header.h):
    Code:
    void foo(int);
    int bar(char*,int);
    Source (source.c):
    Code:
    #include "my_header.h"
    
    void my_function()
    {
        int blah = 10;
        char array[8];
       /* Call other functions */
        foo(blah);
        bar(array,sizeof(array));
    }

    When you go to compile this, the preprocessor first takes care of its business and generates an intermediate file with the contents of the header pasted into the source file as such:
    Code:
    void foo(int);
    int bar(char*,int);
    
    void my_function()
    {
        int blah = 10;
        char array[8];
       /* Call other functions */
        foo(blah);
        bar(array,sizeof(array));
    }
    This is what the compiler works with to generate its object code. The linkers job is to combine these various object files with other bits of compiled object code (libraries or other source files that are a part of the current project) into a final executable program.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static variables and header files
    By drrngrvy in forum C++ Programming
    Replies: 8
    Last Post: 12-02-2006, 12:27 PM
  2. Error Handling and Header Files
    By the pooper in forum C Programming
    Replies: 10
    Last Post: 01-03-2005, 12:40 AM
  3. Borland 5.5 Header Files and Libraries
    By Zoalord in forum C++ Programming
    Replies: 5
    Last Post: 09-12-2003, 07:48 PM
  4. Missing header files and libraries with Borland
    By quagsire in forum C++ Programming
    Replies: 5
    Last Post: 03-26-2003, 05:19 AM
  5. Linking .h files to libraries
    By sean in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2001, 08:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21