473,406 Members | 2,713 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,406 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 2427
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...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.