Thread: standrad library

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    11

    Smile standrad library

    Hi every body!
    Let me introduce myself. My name is sarah . I have been working with cisco routers and switches for 5 years. I really want to learn programming in c++ . I learned all about networking through cisco side called net pro and cisco press bokks, and my home lab.
    I decided to use the technique to learn c++: a good side where i can post my questions and get answers from experts and good C++ book .

    Here is my first question.

    1)What is the purpose of standard library?

    Here is my understanding:
    Rather than writing function for every thing which will result in too much complexity , a programmer can use appropriate header file from the standard library to perform the desired task. For example if i have to find the log for certain number, i can simply use an appropriate header file from the standard library which contains the desired function to find the log. Is my understanding correct?
    2)Is there a good link listing the all header files in standard library for c++ ?
    3)Does standard library in c++ contain only header files?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sarahr202
    Rather than writing function for every thing which will result in too much complexity , a programmer can use appropriate header file from the standard library to perform the desired task. For example if i have to find the log for certain number, i can simply use an appropriate header file from the standard library which contains the desired function to find the log. Is my understanding correct?
    Yes, but of course this applies generally to libraries, not just the standard library. It is not so much that "writing function for every thing (...) will result in too much complexity", but that continually re-writing code that can be placed in a library for reuse is tedious and a waste of time. Also, while you include the header files to get the necessary declarations and definitions, the actual implementation is not necessarily in the header.

    Quote Originally Posted by sarahr202
    2)Is there a good link listing the all header files in standard library for c++ ?
    I am not sure about all, but cppreference.com can come in handy. Personally, I have a PDF copy of the C++ Standard, and that does indeed list all the standard headers and their contents (at least the gist of it).

    Quote Originally Posted by sarahr202
    3)Does standard library in c++ contain only header files?
    No.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    11
    Thanks for your reply.

    You mentioned
    "the actual implementation is not necessarily in the header."

    Then where is the actual implementation if not in the header file?

    thanks and have a nice week end!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sarahr202
    Then where is the actual implementation if not in the header file?
    A source file that has probably been compiled into an object file and lurking in some library archive.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    11
    Thanks for your reply!

    your response to my question"3)Does standard library in c++ contain only header files?"

    was " no"

    Then what else library could possibly contain besides headers files ?

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    A large part of what header files contain are function prototypes. These simply say to the compiler "here is a function called X with arguments Y1, Y2, Y3 and that returns a type Z". This then enables the compiler to do it's job of type checking the arguments you pass into the function (and receive back from the function via the return value) in order to complain when you do something stupid such as passing a struct pointer to a function that expects a double. You could do away with using headers but then you'd manually have to include every prototype for every individual function you use and that would be painful. Headers make things easier in this respect. As for the code itself, this is - as mentioned by laserlight - compiled into object files and stuffed into a library somewhere that gets linked into your application. Sometimes this (the linking to the necessary library) is done implicitly for you and other times it requires explicit referencing in your project's settings.

    Templated code is different in most implementations, templates (in these implementations) require that the code be visible at the point of compilation and thus the headers for these will contain the actual code that will be compiled.
    "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

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    11
    Thanks Charles !

    Here is what i find bout" function prototype".

    "A function prototype in C or C++ is a declaration of a function that omits the function body but does specify the function's name, arity, argument ..."


    so header files just contain function prototype. The code( function body) is defined elsewhere in library .

    My question is when i use the header files say :

    #include <cmath>
    double exp( double arg );

    I just include the header file which contain the function prototype not the function body. So how does compiler locate the function body to perform the desired task .

    thanks a lot and have a nice weekend!

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Technically, it is not the compiler that locates the function. What the compiler does is generate some code to call the exp() function, it doesn't actully NEED to have the function implementation (function body). Another component, called a linker (or sometimes a loader), which combines the compiler generated object file(s) with any library functions.

    These functions are in a file called something.lib or something.a depending on the OS.

    The library file is a collection of object files, containing the actual code to do printf, exp, etc, etc. And the linker will pick out the ones it needs from the library.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sarahr202
    so header files just contain function prototype. The code( function body) is defined elsewhere in library .
    You can understand it that way for now, but note that your statement is inaccurate as it does not describe the full picture. For example, the implementation of inline functions would typically go in the header as well, along with class definitions and template code, as mentioned by hk_mp5kpdw.

    Quote Originally Posted by sarahr202
    So how does compiler locate the function body to perform the desired task .
    That is typically the job of the linker. Either way, you would be responsible for telling the tool where to find the object file to link. You might do it implicitly, e.g., just invoking the compiler without a specific option might eventually lead to the linker automatically linking with the standard library. Or you could do it explicitly, e.g., by invoking the compiler with an option such that the linker will eventually link with the library that you specified.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    11
    Thanks a lot! I really appreciate all the help from you guys.

  11. #11
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by sarahr202 View Post
    Thanks Charles !
    That threw me for a loop a bit there. I was thinking "Who's Charles?" That's not my name, just the name of the guy that I've quoted in my sig.
    "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

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    11
    Sorry about it ! i thought it was your name.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's an import library?
    By chiefmonkey in forum C++ Programming
    Replies: 1
    Last Post: 06-19-2009, 05:00 PM
  2. Property Set Library (PSL) - Announcement
    By vultur_gryphus in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 05-29-2008, 06:04 AM
  3. Makefile for a library
    By sirmoreno in forum Linux Programming
    Replies: 5
    Last Post: 06-04-2006, 04:52 AM
  4. very weird .h problem
    By royuco77 in forum C++ Programming
    Replies: 1
    Last Post: 09-11-2005, 07:55 AM
  5. better c string functions
    By samps005 in forum C Programming
    Replies: 8
    Last Post: 11-04-2003, 01:28 PM