Aleksandar_B said:
I have a few question regarding some essentials concepts in c++ and
would appreciate it if somebody could explain them to
me:
1. What is object code? I know that it's a result of compiler
'translating' source code and that it's a binary file, but what is the
difference between it and executive
file?
2. What is a linker and what is it's connection to object code file and
executive
file?
3. What is a function in the source
code?
4. What is an object in the source
code?
5. What is a library, what does it
contain?
6. What is a header file, what does it
contain?
7. What is the connection between a library and a header
file?
8. What is a namespace and what is it's connection between a library and
a header
file?
I know that these quesions are about the basics of C++ and information
about them can be found 'anywhere', but so far from what I have read
I'm having hard time to 'visualize' these concepts, so some help would
be
great.
--
Windows 7
32bit
Ubuntu 10.04 32bit
Aleksandar_B said:
I have a few question regarding some essentials concepts in c++ and
would appreciate it if somebody could explain them to me:
A good beginners book on programming should cover these questions in more
detail than we can do here, but I will give an introduction.
1. What is object code? I know that it's a result of compiler
'translating' source code and that it's a binary file, but what is the
difference between it and executive file?
Compiling a C++ program is done in three steps:
1. Preprocessing. In this step, a source file is transformed to a
translation unit (TU) by resolving all #include directives
2. Compiling. In this step, the TU from the first step is converted from C++
code into an object file that contains the code in a format that the
processor can understand.
3. Linking. Step 1 and 2 are performed separately for every source file. In
this step, all the object files are combined together to form a single
executable.
So, object code is the result of compiling a single source file.
An executable is the result of combining several object files and (static)
libraries such that the operating system can run the program without running
into the problem of "where can I find the definition of that next function".
2. What is a linker and what is it's connection to object code file and
executive file?
see above
3. What is a function in the source code?
Within C++, a function is a named group of statements that together perform
a task.
In C++, execuable code must exist only within functions.
For a more general definition of a function, see
http://en.wikipedia.org/wiki/Function_(computer_science)
4. What is an object in the source code?
The term 'object' can have different meanings in C++.
Sometimes, the term 'object' is used to refer to a piece of memory that
contains some values. In this sense, object is sometimes used
interchangeably with the term 'variable', where variable can also be seen as
a named object.
At other times, the term 'object' with the same meaning as in Object
Oriented Programming, meaning an instance of a class.
See also
http://en.wikipedia.org/wiki/Object-oriented_programming and
http://en.wikipedia.org/wiki/Object_(computer_science)
5. What is a library, what does it contain?
A library is essentially a collection of already compiled source files that
provide facilities (functions, classes, etc.) that are useful to multiple
programs.
6. What is a header file, what does it contain?
As a C++ compiler processes only one source file at a time and because there
is no link between the name of a source file and its contents, there needs
to be a way to tell the compiler "I will be using these functions and
classes, but they will be defined elsewhere."
This can be done by mentioning all the relevant functions and classes in
your source file, but if you have several source files that all want to
refer to the same functions foo() and bar(), then that becomes tedious and
error-prone. For classes it is even worse, because you would have to type
the whole class each time.
This is where header files come to the rescue. They already contain the code
that tells the compiler "these functions and classes are available
(somewhere) and can be used in this way".
With the directive
#include "my_header.h"
you tell the preprocessor to pull in the contents of the my_header.h header
at that point in the source file.
7. What is the connection between a library and a header file?
Header files are the mechanism by which you tell the compiler "these
functions, classes, etc. are available for use in the program. The exact
function definitions can be found elsewhere."
Libraries are used to tell the linker "if you need function X, here is where
you can find it."
So, header files are used to indicate WHAT is available and libraries tell
WHERE it is.
8. What is a namespace and what is it's connection between a library and
a header file?
There is no direct connection between a namespace and a library or header,
but it is not unusual for a library to provide a namespace with the same
name as the library.
Namespaces are a mechanism to overcome the problem that different libraries
may have used the same word for different concepts.
For example, the term 'vector' is an important mathematical concept, so you
are likely to find a class vector in a math library.
But the C++ standard library also uses the term 'vector' for a resizeable
array.
So, how can you use both kinds of 'vector' in one program? This is what
namespaces are for, because they give contextual information to tell which
'vector' you mean.
Bart v Ingen Schenau