473,372 Members | 1,007 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

Linking with library

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?

Mar 20 '08 #1
2 2423
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
Mar 20 '08 #2
Jim Johnson <ao*******@yahoo.comwrote in
news:d6********************************@4ax.com:
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
Mar 20 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Steven T. Hatton | last post by:
Is there anything that gives a good description of how source code is converted into a translation unit, then object code, and then linked. I'm particularly interested in understanding why putting...
20
by: Steven T. Hatton | last post by:
I just read this in the description of how C++ is supposed to be implemented: "All external object and function references are resolved. Library components are linked to satisfy external...
2
by: | last post by:
Help! I'm new to c++, and am breaking my teeth on MS Visual C++ (bundled within Visual Studio .NET 2003). Am trying to link simple c++ code to fortran dlls created in Compaq Visual Fortran (v6.1)....
7
by: wmkew | last post by:
Hello everyone I'm encountering a R6002 Runtime error and several bugs when trying to generate a simple Managed C++ application with .NET 2003. The main problem seems to arise from linking with...
6
by: Rudy Ray Moore | last post by:
I work with a multi-project workspace. One project (the "startup" project) has a "Configuration Type" of "Application (.exe)". The other 40 projects have a "Configuration Type" of "Static Library...
1
by: Kay | last post by:
I already specified to ignore specific library: MSVCPRT.lib MSVCRT.lib LIBC.lib MSVCRTD.lib LIBCD.lib command line is like: /INCREMENTAL /NOLOGO /DLL /NODEFAULTLIB:"MSVCPRT.lib MSVCRT.lib LIBC.lib...
10
by: Julian | last post by:
I get the following error when i try to link a fortran library to a c++ code in .NET 2005. LINK : fatal error LNK1104: cannot open file 'libc.lib' the code was working fine when built using...
0
by: Philip Lowman | last post by:
I am in the process of trying to migrate a couple of build solutions to Visual Studio Express 2005 from VS 2003 Professional and I am running into a weird C/C++ runtime library linking issue when...
0
by: xieml2007 | last post by:
Dear Madam or Sir, I encountered one problem which is quite similiar to the discussions launched at the web site: http://www.thescripts.com/forum/thread280324.html
2
by: Mohsen A. Momeni | last post by:
Hi, Does it have any difference in security, whether to compile a function as a static lib and link it with a program or just add the function to the source? In other words, suppose we have two...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.