How does compiler/linker such as GCC decide to start locating the appropriate library

This is a discussion on How does compiler/linker such as GCC decide to start locating the appropriate library within the C Programming forums, part of the General Programming Boards category; I am a newbie in the programming world My question is on how the compiler/linker such as GCC locate the ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    4

    Smile How does compiler/linker such as GCC decide to start locating the appropriate library

    I am a newbie in the programming world

    My question is on how the compiler/linker such as GCC locate the appropriate library?

    I know that header files (.h files) are just merely going to be extracted by the PreProcesser before compilation onto the source file.

    After the preprocessing, it's just the source code with the function declarations inserted before the main() function.

    When that source file is compiled successfully. Then, the Linker will link unresolved symbols with other object files or/and with the library.

    How does the Linker know which object file contain the particular symbol that it was looking for ?

    maybe the user manually have to include necessary object files such as,

    gcc a.o b.o c.o -o output

    the error will occur at Linking phase if only

    gcc a.o b.o -o output was executed.


    Which means we included the necessary files all into one gcc command as shown above ..

    But what about #include <stdio.h> , and so on.. (defaultly included ones)..

    I know that linker will search default librarys... but how does it know which .a file contains necessary .o file ?

    the difference between < > , and " " for declaring header files are merely nothing, but location issue..

    You can change "a.h" to <a.h> by indicating the header file location

    gcc a.o b.o -o output -l. -L.

    No difference between < >, " " from gcc point of view..
    How does it know whether to start looking for particular .o file in the standard library, and how does it know what .a contain that .o , and so on....

    And how do we always have to include our own source files in gcc compilation, while default standard library are "somehow" automatically triggered for search...

    Help me on this concept...
    Thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    the standard C library (libstdc, or libstdc++ for C++) is included by gcc when gcc decides to produce an executable. Other libraries that you may need (e.g. if you include <math.h>) must be specified by you.

    And note that there is a difference between "" and <> in that gcc looks for "" files FIRST in the local directory, whilst <> are NOT searched for in the local directory unless you specify "-I." specifically. And this distinction is important for the reader of the source-code - it basicly helps the reader to understand that <This is NOT part of the current project> vs. "this is part of the current project". No, the compiler doesn't care the least bit about which you use.

    -L. means look for libraries in the current directory, which may be useful if you produce parts of your application as a library file first, then link that to the main application.

    --
    Mats

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by shong9 View Post
    How does the Linker know which object file contain the particular symbol that it was looking for ?
    How would you find a piece of information in a collection of books? You look inside them.

    I know that linker will search default librarys... but how does it know which .a file contains necessary .o file ?
    The .a files have an index in them.

    the difference between < > , and " " for declaring header files are merely nothing, but location issue..
    Header files have NOTHING to do with linking.

    And how do we always have to include our own source files in gcc compilation, while default standard library are "somehow" automatically triggered for search...
    Because having to manually link the C library every time would be a pain in the ass.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you run gcc with the switch -v it will show you what commands it performs in each step - it will print quite a lot of information, but you will be able to see that "ld" (which is the linker) is called with "-lstdc" or something similar.

    --
    Mats

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    4

    thanks

    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 02:30 PM
  2. Makefile for a library
    By sirmoreno in forum Linux Programming
    Replies: 5
    Last Post: 06-04-2006, 05:52 AM
  3. Next Question...
    By Azmeos in forum C++ Programming
    Replies: 3
    Last Post: 06-06-2003, 03:40 PM
  4. Need some help with a basic tic tac toe game
    By darkshadow in forum C Programming
    Replies: 1
    Last Post: 05-12-2002, 05:21 PM
  5. win32 apps compiled with GCC start console window!
    By Citrus538 in forum Windows Programming
    Replies: 5
    Last Post: 02-18-2002, 10:35 PM

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