Linking with library

Discussion in 'C++' started by jason.cipriani@gmail.com, Mar 20, 2008.

  1. Guest

    On Mar 20, 12:32 am, Jim Johnson <> wrote:
    > C++ linking works by using Linker to combine object files into single
    > exe file.
    >
    > Does each .cpp file compile to a single .obj file?


    Generally, yes.

    > If the .cpp code called functions in some library (.lib) does the
    > Linker will extract the required .obj object file from the library and
    > link together with other .obj file to form a EXE?


    Yes. Although some linkers may not discard unused object files from
    the library; or may give you an option to make it not discard unused
    ones (which can sometimes be useful if you want your exe to also
    double as a dynamic link library [on Windows the file formats are the
    same], exporting functions from linked in libs even though it doesn't
    use those functions itself).

    >
    > So the EXE file can be think of a code file that merged all the .cpp
    > (compiled to object) files together?


    Pretty much. Generally the way it works is each source file compiles
    to an object file, and each of those object files contains a table of
    external symbols that it requires (such as functions called from that
    source file that are defined in other source files or libraries). The
    linker then goes through all of the object files, looking at the
    external references each requires, and resolves the external
    references by finding what other object files (some of which may be
    in .libs) export those symbols and linking it all together, doing some
    magic, and producing a final executable.

    Jason
     
    , Mar 20, 2008
    #1
    1. Advertising

  2. Jim Johnson Guest

    C++ linking works by using Linker to combine object files into single
    exe file.

    Does each .cpp file compile to a single .obj file?

    If the .cpp code called functions in some library (.lib) does the
    Linker will extract the required .obj object file from the library and
    link together with other .obj file to form a EXE?

    So the EXE file can be think of a code file that merged all the .cpp
    (compiled to object) files together?
     
    Jim Johnson, Mar 20, 2008
    #2
    1. Advertising

  3. Paavo Helde Guest

    Jim Johnson <> wrote in
    news::

    > C++ linking works by using Linker to combine object files into single
    > exe file.
    >
    > Does each .cpp file compile to a single .obj file?


    Normally, yes.

    > If the .cpp code called functions in some library (.lib) does the
    > Linker will extract the required .obj object file from the library and
    > link together with other .obj file to form a EXE?


    It may extract only single functions (and their dependencies) from a
    static lib or (next to) nothing from a dynamic lib. In the latter case
    the lib must be available at run-time, the actual linking then occurs by
    loading the dynamic lib into the memory, or later. Exact rules depend on
    the platform. (For example, the .obj and EXE extensions you mention are
    specific to a certain platform.)

    >
    > So the EXE file can be think of a code file that merged all the .cpp
    > (compiled to object) files together?
    >


    In old times, yes. Currently the widespread usage of dynamic linking and
    more recently link-time optimization and exported templates generation
    have made the process much more complicated.

    Best
    Paavo
     
    Paavo Helde, Mar 20, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roland Raschke
    Replies:
    1
    Views:
    1,097
    Kevin Goodsell
    Sep 22, 2003
  2. Albert
    Replies:
    4
    Views:
    519
    Mike Wahler
    Sep 29, 2003
  3. Josep

    Dev-c++: Linking a library

    Josep, Mar 7, 2004, in forum: C++
    Replies:
    1
    Views:
    3,817
    John Harrison
    Mar 7, 2004
  4. Anthony Yio

    Linking to Static library of C

    Anthony Yio, May 20, 2004, in forum: C++
    Replies:
    1
    Views:
    356
    John Harrison
    May 20, 2004
  5. Thomas Ruschival

    C++/C-library linking (pslib)

    Thomas Ruschival, Nov 13, 2004, in forum: C++
    Replies:
    3
    Views:
    578
    Thomas Ruschival
    Nov 14, 2004
Loading...

Share This Page