Why do we use libraries?(Sorry but i am a newBei)

S

Saurabh

Hi all,

I want to know why do we use libraries(static or shared).
We can perform that work by including header files too.I also wish to
know what exactly is linking and loading.
i know these are pretty easy and basic questions.
But plz help me considering the fact that i am a new developer
in C++.
Thanking you in advance.


Just Saurabh
 
D

Divick

I want to know why do we use libraries(static or shared).
Did you try google?Header files contains only the declarations(or at least are supposed
to), but not definitions. Static / dynamic libraries, contian the
definitions i.e the implementation of functions.

HTH,
Divick
 
S

Saurabh

Divick said:
Header files contains only the declarations(or at least are supposed
to), but not definitions. Static / dynamic libraries, contian the
definitions i.e the implementation of functions.

HTH,
Divick

Thanks a lot Divick.
Ya i tried google.But couldnt get what i was looking for.
> Header files contains only the declarations(or at least are supposed
to), but not definitions. Static / dynamic libraries, contian the
definitions i.e the implementation of functions.
But we can define functions in header files too.
Files like <stdio.h>, <iostream.h> contains declarations
as well as definitions.
So why do we bother to create libraries?
I mean what benefit we get by creating them.
Just Saurabh
 
M

Marco Wahl

Saurabh said:
I want to know why do we use libraries(static or shared).

One answer is: because the use of libraries can make
programmers life much more convenient. A library
contains and provides some functionality that can be
used without thinking much about its implementation.

Take for example the functionality of output in C++.
If you want to write 42 to the output you simply
include the header-file 'iostream' and use a suitable
construct from the library. E.g.

[[[
#include <iostream>
int main() {
std::cout << 42 << std::endl;
return 0;
}
]]]

Isn't that cool? Imagine you should have written that
output-functionality on your own. You could regard
libraries as time-savers. (Ok, they may bring new
library-linking-specific problems. ;-) )

BTW typically there is no need to specify the
respective library that contains the functionality of
the std-namespace in the build-process.
We can perform that work by including header files too.I also wish to
know what exactly is linking and loading.

Linking is the process of bringing a library and other
code together. Linking is the realization that a
program actually uses the functionality of a library.
i know these are pretty easy and basic questions.

How do you know? ;-) Do you already know the answers
and this is kind of a test-if-the-others-know-too-question?
But plz help me considering the fact that i am a new developer
in C++.

Keep your eyes open! ;o)


HTH
 
W

Walter

common libraries can be shared by many applications.
sometimes, you can only provide others libraries without showing your
source code. etc.
 
J

Jim Langston

Saurabh said:
Hi all,

I want to know why do we use libraries(static or shared).
We can perform that work by including header files too.I also wish to
know what exactly is linking and loading.
i know these are pretty easy and basic questions.
But plz help me considering the fact that i am a new developer
in C++.
Thanking you in advance.

A library contains pre-compiled code that is executable. When you link to a
library file, your program will call the functions contained in that library
and execute them. Your program only has to know how to call the library
function and what it returns.

A header file can contain just about anything, but one common thing they
contain is linking type information, including how to call library
functions. If you want to call function abc() in library xyz.lib you have
to know how to call it. What parameters does function abc() take, and what
does it return? The header may contain something like:

extern int abc(int, float, double);

Which only states how you would call abc. It may or may not have the extern
(not sure if it's needed), don't think it is but can't remember. As you can
see this has no code for the function abc, that's already been compiled by
the people who made the library and given to you. You don't know what
function abc does other than any documentation you can scrounge up on it.

Hope that helps.
 
N

Nathan Addy

Two of the big reasons are quicker compilations and smaller binaries.

So as you know, there isn't any reason why you can't include all the
source files or every program you write and compile it all at once. An
example will show why that would be BAD. Suppose I am a beginning
video game programmer using some fancy graphics library. The very
first thing I do when I sit down to write Doom 4 is to write a skeleton
application that says something like

#include <mygraphicslibrary.h>
int main()
{
MyGraphicsLibrary mainGraphicsObject; //line 1
return 0;
}

and compile it. Normally, you just include the header, compile your
file, and then link an already compiled library of mygraphicslibrary,
and it works and is very fast.

What goes on behind the scene while it's compiling your file is that
the compiler sees the line "MyGraphicsLibrary mainGraphicsObject" and
also sees that the header file says this is legal code (by saying there
is a MyGraphicsLibrary class and saying that that class has a default
constructor). So now the compiler knows there is compiled code
somewhere that will "fit" into the function "holes" in your file that
will make everything work(remember the header typically just says what
code is legal, but doesn't say anything about what that code actually
does or how it works). So what happens is that your file is compiled
in a way that will make it possible to go back and connect up the
missing functions(that connecting is the linking step) Because of this,
to the compiler, compiling that file is about as much work for a
compiler as compiling the file
int main()
{
int x;
return 0;
}

This is because the compiler doesn't have to do anything except know
that some object is created and that linking will provide a pre-written
function to do that. So your program will compile in one or two
seconds. When you link it to the MyGraphicsObject library, the code
you have just compiled says "I need a default contructor for a
MyGraphicsLibrary object" and the library says "I have a default
constructor for a MyGraphicsLibrary object". The linker can quickly
connect the two together and the whole process(compiling and linking)
should take just a second or two.

If, however, you decided to compile the code all at once, you would
probably find that the graphics library is thousands of lines long, if
not tens of thousands. It likely would end up taking hours to compile
your simple program. Additionally, each time you wrote a new program
with this graphics code, you would have to redo this long, long
compilation. I am currently working on a medium sized program that
takes about 40 minutes each time I need to recompile from scratch. If
I didn't link in already compiled versions of the libraries, it
possibly could take days. So speed is one huge reason.

Size is another reason for using shared libraries. So now suppose you
are writing some new string library. And this library is going to be
so great that everyone who writes programs is going to use it. Maybe
you write a couple programs that use this library and you want to know
why you should link this to a shared library or a static library. In a
static library, you compile your string code and then any program that
needs to use that string code basically copies the static library into
it's own space. So each program that you write now includes your
string code and has a larger size because of it. Maybe even more
important is that each program runs its own copy of that string code.
So that's a lot of extra disk space and a lot of extra memory that all
those programs use. The alternative is to link your program to a
shared library. Then each program is able to share the same code. So
the programs all stay smaller(because they use the community copy,
instead of making a copy for themselves). They also use less memory,
because the string code only has to be loaded once, which all programs
then share, instead of loading one for each.

A final reason is for making closed-source code. Supose I owned a
programming company and made graphics libraries that were intended to
be used by other programmers. But I want to keep my code secret, so
that other people can't copy or steal it. So the problem here is how
do I give people code so that they can see exactly what the program can
do without there being any way for them to find out how it does it.
Libraries are the way to do this. I can give you header files(what it
does) and a library file(how it does it), and you will be able to use
the software perfectly, however, you won't know anything about how it
works other than what it does.

Anyways, this was very long, but I hope it helped. Libraries aren't
terrible complicated, but a lot of books and stuff doen't really
explain them all that well and it makes them seem much more complicated
than they need to be. Hope it helped.
 
D

Divick

But we can define functions in header files too.You can definitely have the full definition within the header file, but
wherever you include the header file, the code will get duplicated,
because the preprocessor will simply replace the #include <iostream>
with the contents of iostream and thus this is generally avoided in
libraries containing functions which are large or functions which do
some significant work rather than just print. For this reason only you
have dlls/and DSO (on winXX/*nix) to avoid duplication of code in the
final executable as other have also pointed out.

Divick
 
H

hnaik

Corrections:
..h always contain declarations
if they contain definitions, thatz an error.
Definitions exist in .c files (if you see the source)
After you compile the libraries they exist as .o (object) files (so
that can be linked)

Libraries exist such that different applications can use(link to)
commonly used code.

Consider a situation:
You are writing a simple hello world program with only one printf
statement.
Imagine you are distributing the binary to friends.
Would you like to have:
1. your code
2. printf's definition code
3. lots of other system code
in your binary, to make your binary 10 times bigger that it should
actually be.

instead you just distribute your hello world code.
your friends link it to the stdio library on their system (which has
the definition of printf)
and use your code.

I would advise you to read up a System software book.

Regards,
Harish
 
B

benben

Nathan said:
Two of the big reasons are quicker compilations and smaller binaries.

In addition to that and what everyone says there is another fundamental
reason why library is critically important: to provide accessibility to
code that can't be written in the language.

For example, if your program is running on top of an operating system,
the way you access to low level IO and system services is via a system
call, which typically involves a software interrupt. Yet C++ knows
nothing about interrupt and hence a system call cannot be done directly
in C++. Usually, you have function stubs that have a C++ interface but
are implemented in other languages that performs the interrupt for you.

Another example is C++ may not be the most efficient language to do some
particular tasks. Such tasks are best done by other specialized
languages. To facilitate interoperability with C++, they can provide a
C++ interface to what is compiled from other languages.

Regards,
Ben
 
C

Christian Gollwitzer

hnaik said:
Corrections:
.h always contain declarations
if they contain definitions, thatz an error.

What about

------------myfile.h----------------
#ifndef MYFILE_H
#define MYFILE_H
template <typename T>
inline T SQR (const T& x) {
return x*x;
}
#endif
------------------------------------

is this a declaration or a definition?

Why do you think that included files may not contain definitions? (I
know it can lead to duplicate symbols when linking several files, but it
is not forbidden)

Christian
 
S

Saurabh

Hi all,

I am really very much thankful to all of you.
Jim ,Divick,marco,nathan ,Harish Everybody.
This info is really useful for me.
and ya..

marco ,believe me,this was not a "test-if-others-know-it-too" .
i really needed this info.

Thanking you all once again.

Just Saurabh
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top