Thread: Header and Library Files

  1. #1
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256

    Header and Library Files

    What is the difference between header files and library files? More specifically, what roles do they have in compilation/linking process?
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  2. #2
    the Wizard
    Join Date
    Aug 2004
    Posts
    109
    If your fx. look into an header file, they contain a lot of definitions and such, which refers to a (as far as I remember, somebodt correct me if I'm wrong please ) to a library file, which then knows what to do. When you fx. call a sinus(sin) function from the header file math.h..

    That is fx. why you can't just copy & paste a header file from one compile to another and make it work, because it's the library that really has the code and not the header file...

    It's also possible to make your own header file, if you want to make your code more (wrong word, but can't think of another) "pure"/"readable"..

    Some library file examples could be glut.lib which again holds some new functions for GLUT programming or opengl.lib as another..

    Hope this helps you a little?? If not, just write again
    -//Marc Poulsen -//MipZhaP

    He sat down, he programmed, he got an error...

  3. #3
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    what is fx.?
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  4. #4
    Registered User lobo's Avatar
    Join Date
    Oct 2001
    Posts
    71
    Header file declares things - it says compiler that things you use in code (functions, defined names etc.) do exist, that they are defined, somewhere. Then, compiler can say your code is syntactically correct.
    Later, such things as functions' bodies are expected to be found somewhere, so that linker can put your code together. So it looks for 'bodies' of those functions declared in headers, either in your provided source files or in specified libs (they are, basically, something like precompiled pieces of code). Function bodies in libs are linked with their respective calls in your source files.

    Hope this does not collide with MipZhaP's post much and clears it a bit

  5. #5
    the Wizard
    Join Date
    Aug 2004
    Posts
    109
    fx = for example
    lobo, thx for your support It doesn't conflict with what I wrote
    -//Marc Poulsen -//MipZhaP

    He sat down, he programmed, he got an error...

  6. #6
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    from what I understand from the explination, header files contain function declarations/prototypes and library files contain function definitions?
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Correct.

  8. #8
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    Thank you sean, could that be your shortest post?

    Cool! Some other questions:

    1) Why is there both a library and a header file? Couldn't the header contain the function definitions as well?

    2) Does the library file contain only the function definitions which are used in your source code? If so, how does it get created with only the definitions of the functions used?

    edit: The answers have come to me!
    Please ignore question #1, and #2.
    Last edited by jrahhali; 08-22-2004 at 10:33 PM.
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    1) I suppose so but that's very bad practice. All the commercial (and open source, mind you) libraries are packaged as a library of code and a header of the prototypes the user will need to know.

    pre-edit-edit: Just saw your edit...

  10. #10
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    MipZhaP
    a header file, they contain a lot of definitions and such, which refers to a...library file, which then knows what to do.
    I examined the header iostream.h (turbo c++) and i was trying to find a referal to a library file but i couldn't.

    edit: Why?
    re-edit: I just went on a chat room, and I was told that the linker searches through all the library files for the definitions, not just one that the header file specifies. Which answer is right (refering to the quote above)?
    Last edited by jrahhali; 08-23-2004 at 11:28 PM.
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. header and source files
    By gtriarhos in forum C Programming
    Replies: 3
    Last Post: 10-02-2005, 03:16 AM
  2. include library header in header files
    By Raison in forum C++ Programming
    Replies: 6
    Last Post: 09-27-2004, 02:50 AM
  3. Where to get library files?
    By klear in forum C++ Programming
    Replies: 7
    Last Post: 03-29-2004, 02:15 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Using c++ standards
    By subdene in forum C++ Programming
    Replies: 4
    Last Post: 06-06-2002, 09:15 AM