On Mar 20, 12:32 am, Jim Johnson <aopiyy...@yahoo.comwrote:
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