#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Location
    Hyderabad
    Posts
    17
    Rep Power
    0

    Linakge of Standard Library Functions


    Hi,

    In c how the linkage of Standard Library Functions happens?



    Please explain me.


    Suri
  2. #2
  3. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    13
    well you know it's called by an include:

    #include <stdio.h>

    which go at the top of the source file. that makes the compiler look where it knows libraries are stored. when you give it an include to look for and if the file name is in < > brackets it'll try and find it in the standard place. any functions that are in stdio.h are then useable by the code that follow beneath the include.

    if the file name is enclosed in quotes like:

    #include "header.h"

    it'll look first in the same directory as where your source code is for that file, if it doesn't find it there it'll then look for it in the standard place.

    that's how i understand it anyway. hth.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Location
    Blacksburg VA/Philly PA
    Posts
    38
    Rep Power
    13
    Originally posted by balance
    well you know it's called by an include:

    #include <stdio.h>

    which go at the top of the source file. that makes the compiler look where it knows libraries are stored. when you give it an include to look for and if the file name is in < > brackets it'll try and find it in the standard place. any functions that are in stdio.h are then useable by the code that follow beneath the include.

    if the file name is enclosed in quotes like:

    #include "header.h"

    it'll look first in the same directory as where your source code is for that file, if it doesn't find it there it'll then look for it in the standard place.

    that's how i understand it anyway. hth.
    yep that's pretty much how it works. once the function you are calling is found it pretty much inserts the code from the called library function into your code.
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    Nope. Nice try, but that isn't quite it. Try compiling the following:
    Code:
    #include <stdio.h>
    int main(void) {
       printf("Hello world\n");
       return 0;
    }
    Now remove the #include <stdio.h> and try compiling again. Notice that the program compiles JUST fine and runs the SAME way.

    So, what's the deal then and what exactly is in this header file <stdio.h>. Well, if you open it, you'll notice the only thing that it contains is a few #define and #include lines (defining things like TRUE, FALSE, NULL etc. and possibly including other .h files ), and function prototypes. So where's the actual function code then?? The answer is very simple. All C compilers come with a bunch of libraries and the linker looks at those libraries by default, unless you specify alternative libraries to the linker. The standard library is usually called libc.a (for *NIX), libc.lib (for Visual c++), CS.LIB or CL.LIB (for Turbo C) etc. The linker that comes with the C compiler will automatically look at the standard libraries BY DEFAULT unless told otherwise specifically. All that the header files have are function prototypes, so that the compiler may know ahead of time, to typecast arguments to the correct types, if needed. If a C compiler can't find the function prototype, it'll assume that you've called it with the correct number/type of arguments and leave it to the linker to link them together! This explains why our sample hello world program works whether you include stdio.h or not.

    So how are these libraries created. Basically, when you compile C programs, this is how the process works:
    { C Source Files } ==> Compiler ==> {Object Files}
    {Object Files} + { Third Party Object Files } + { Libraries} ==> Linker ==> {Executable}
    The C compiler takes C source files and compiles them into Object files. An object file contains machine code + stub information to enable the linker to join the functions together to form the final executable. Hence, third party people can distribute only the object files and the header (.h files), and you can still create an executable using their object files. However, if you have a large number of object files, then they become a little unmanagable, especially, if you implement one or two functions per file. So, you can combine a bunch of object files into a library using a library tool. Depending on the compiler, the name of the tool varies. For example, Turbo C has TLIB.EXE, Visual C++ calls it LIB.EXE, C++ Builder has a wizard etc. You can use the library tool to take a bunch of Object files and organize them into a library. The standard library is built essentially the same way.

    Hope this helps!
  8. #5
  9. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    13
    that's great information. certainly helps me.

    further question:

    how could i find out all the connected library files? find out what/where they all are?

    the reason i ask is because i am attempting to make an all in one easy to install version of gnu's gcc (for mac os x). i'm pretty naive on the whole thing, and after finally managing to build and install gcc (using apple's gcc that comes with apple's dev tools) i then copied over the freshly installed gcc to another machine (that didn't have dev tools or gcc) and found much to my surprise, but probably not to yours, that compilation needs more than just a compiler.

    so i need to find out what else is needed, that a development environment has, that a standard environment doesn't have (apart from the compiler obviously)

    i've found out i need an assembler and linker apparently. also the standard c libraries as they're also not included in a gcc install. how can i find out which libraries are needed by gcc? is that possible?

    how did you make the connection between stdio.h and libc.a (or different name for different system)? or is it just something you picked up rather than found out via the code/files?
  10. #6
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    >> how could i find out all the connected library files? find out what/where they all are?
    Hopefully this page contains what you need. You also need all the header files under /usr/include, or whatever the equivalent is, under Mac OS X. Usually the library files are in /usr/lib (or /usr/local/lib) and the binaries are in /usr/bin (or /usr/local/bin) on a *nix system, and since OSX has a FreeBSD heritage, it might be in the same place (Don't take my word for it though! :)).

    >> how did you make the connection between stdio.h and libc.a (or different name for different system)? or is it just something you picked up rather than found out via the code/files?
    Comes with experience and writing apps that needed to be linked to libraries other than the standard libraries (this happens a lot, if you're writing code for handheld scanners or other such embedded systems). It's precisely when I was deprived of the standard libraries when I became aware of them :)
  12. #7
  13. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    793
    (this happens a lot, if you're writing code for handheld scanners or other such embedded systems)
    I do the same thing but got past this problem. We just use a telnet client on the scanner that telnets to the application server using 802.11b. Love writting apps that run on our scanners. :)
  14. #8
  15. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    13
    Hopefully this page contains what you need.
    yes that look very useful. it's got 'dependancies' listed which is very usefull. thanks.

IMN logo majestic logo threadwatch logo seochat tools logo