Thread: Libraries and headers

  1. #1
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314

    Libraries and headers

    Sorry for this newbie question, but what do libraries exactly do ? Why do I have to #include a header file *AND* at the same time tell the compiler to link some libraries?

    In the past I've been coding in Delphi and there was no need for libraries, at least not that I am aware of.
    i.e. to get access to all external winsock.dll functions, all I had to do is inlcude the winsock.pas file. This file contained all neccessary data types as well as procedure prototypes that were linked to the appropriate DLL function (i.e. function accept; external WINSOCK_DLL name 'accept'; )

    As far as I can tell with my limited C++ knowledge, a header file contains almost the same data, so what are those libraries used for ? What kind of additional code is neccessary in C++ to access a DLL?

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    A header file only tells the compiler what functions exist inside the libraries. The .lib file will do one of two things. In a static linked library, it will actually contain all of the functions you are trying to access. In a DLL, the .lib will contain the code needed to load the DLL and call the function.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    The header files include very often used methods. Say you want to write some text to the console screen you need to #include <iostream> or if you want to open a file you need to #include <fstream>. if you got alot of spare time you could write your own version but why bother.

  4. #4
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    A .pas is split in interface- and implementation-section. So a c++ header file represents the interface section (procedure prototype to be used in pascal) and the library the implementation part (procedure; external; to link to the library).

    I think I understand that. ... think...

    Oh, just one more thing: Those libraries, can the the compiler extract only the needed data or is the entire thing linked into the exe?

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Compilers generally only extract the needed data AFAIK.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    Ok..... my problem here is: The more I understand, the more questions I have...

    Next problem: Some includes are postfixed with a .h, some are not. Does the .h mean there is nothing but prototypes in the include ? The mingw isn't exactly what I'd call well structured code (at least not for a c newbie), but that seems to be the only difference.

    Another thing is the library directory. It contains files with different extensions. What does .a mean? .o? .la?

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    The only header files that are not prefixed with .h are the new standard C++ libraries, which always use the std namespace. You should never have code in an include anyways.

    .o and .a are compiled files which are ready to be linked. The other one I've never heard of.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Originally posted by XSquared
    You should never have code in an include anyways.
    Actually, in some cases it's vital to put the code in the headers. Templates require this, as do inline functions with many compilers.

  9. #9
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Ok..... my problem here is: The more I understand, the more questions I have...
    Welcome to the wonderful world of programming.



    Libraries are basically code repositories - functions with no main function in them.

    Headers are where function prototypes, class definitions, typdefs, #define's, etc. are.

    Header: foo.h
    Code:
    #ifndef _FOO_
    #define _FOO_
    
    #define TRUE 1
    #define FALSE 0
    
    typedef unsigned int WORD;
    
    void Foo(void);
    int Foo2(int x,int y);
    ...
    ...
    ...
    #endif  //end of foo.h
    Library:
    Code:
    #include "foo.h"
    
    void Foo(void)
    {
    ...
    ...
    ...
    }
    
    int Foo2(int x,int y)
    {
      return (x+y);
    }
    And again Cat is correct. Templates do require code in the headers - at least most of the compilers I've worked with do. I have a post on here about this very thing - somewhere deep down in the dark damp dungeons of the board.

  10. #10
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    ok, thanks for all the replies


    I've come to the point where my program (actually it's the code of NeHe's OpenGL tutorial) has become pretty... large.
    So I'd like to move some functions to a different file. Is there an easier way then splitting up this code into a header and a cpp? Is there any way to include a file "as it is" without the compiler messing around with it?

    I tried to store all functions but main in a different cpp and used include to "link" it to my main app. I got a lot of error messages because of missing datatypes in that new cpp file so I included those standard headers there, too. Now the compiler told me that those types are already defined :/

    Is there something like "uses ..." in c?

  11. #11
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Code:
    #ifndef _FOO_
    #define _FOO_
    
    #define TRUE 1
    #define FALSE 0
    
    typedef unsigned int WORD;
    
    void Foo(void);
    int Foo2(int x,int y);
    ...
    ...
    ...
    #endif  //end of foo.h
    This #ifndef, #define, #endif system will stop the multiple include problem. Your compiler is including files more than once which results in the error you are getting.

    #ifndef _SOME_VARIABLE_ - if this does not evaluate to 1

    #define _SOME_VARIABLE_ - define it and make it evaluate to 1

    ...this will be only included if SOME_VARIABLE evaluates to 0

    #endif - end of the ifndef conditional block

    This ensures that you only include a file when it is needed. If this file has already been included once, _SOME_VARIABLE_ will evaluate to 1 and the code between #ifndef and #endif will not be included again thus preventing multiple declarations.

    Remember that headers only contain declarations for the most part and not actual definitions or bodies of functions - exceptions are templates and inline functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  2. Dev-C++ Headers and Libraries
    By Mithoric in forum Windows Programming
    Replies: 2
    Last Post: 03-12-2004, 03:27 PM
  3. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  4. Headers, Libraries and the rest
    By nickname_changed in forum C++ Programming
    Replies: 8
    Last Post: 06-23-2003, 08:17 PM
  5. OpenGL Headers and Libraries
    By Invincible in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 02-14-2002, 06:37 PM