Help with C++ basics

A

Aleksandar_B

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.
 
A

Angus

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?
A computer CPU does not understand C++ syntax. It only understands
platform specific operation codes. Eg op code for moving a 32 bit
value in memory location 112 could be 17 <source_mem_location>
<target_mem_location> This is not a real op code by the way.

Most programmers don't want to have to learn low level op codes.
Therefore 'high level' languages are used so programmers can work at a
higher level of abstraction. The compiler translates C++ instructions
into assembly code. An assembler converts the assembly instructions
into machine code.

Object code is the machine code. A linker 'links' the bits of object
code together to form an executable program.

The compiler will create an object file for each translation unit
(think that is the term). A translation unit is eg widget.cpp (and
widget.hpp). So for a program you might gets LOTs of object files.
The linker puts all the bits of machine code together to form an
executable.
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?

*** Not sure what your question is?
4. What is an object in the source code?

** Don't confuse a C++ object and object code. They are two entirely
different concepts.
5. What is a library, what does it contain?
*** A library is like object code which contains useful functions (but
in object code form). If for example you use cout, then the
implementation of this will be in a library which your linker links in
with your own code to form an exe. A library can be considered
equivalent to an object file containing your own functions.
6. What is a header file, what does it contain?
*** A header contains the correct signatures for functions which you
might call. Someone better help me out here :)
7. What is the connection between a library and a header file?
** eg to use a vector you #include <vector>. The standard library
contains the implementation of a vector. The linker links the
required code into your exe if you use vector.
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.
*** Not only C++ but any compiled language. Interpreted languages use
slightly different concepts.

I hope this helps.
See above
 
P

Puppet_Sock

I have a few question regarding some essentials concepts in c++ [snip]

Wow, that looked exactly like homework. But I know
that nobody would ever post homework on the webs.

The answer to most of your questions is: google
Socks
 
A

Aleksandar_B

I'm long way from homework, and school for that matter. This is just
something I started doing to pass the time.

But you know what, instead of putting idiotic remarks how about helping
or just skipping a chance to make a little turd on the way.

Homework, yeah right.


Windows 7 32bit
Ubuntu 10.04 32bit

I have a few question regarding some essentials concepts in c++ [snip]

Wow, that looked exactly like homework. But I know
that nobody would ever post homework on the webs.

The answer to most of your questions is: google
Socks
 
A

Aleksandar_B

Windows 7 32bit
Ubuntu 10.04 32bit

*** Not sure what your question is?

So far I'v got that function is smaller independent unit that contains
operations and data, but I'm having trouble visualizing it or
distinguishing it from an object. You mention it in your library definition.
** Don't confuse a C++ object and object code. They are two entirely
different concepts.

Ok, so what is a C++ object?
*** A library is like object code which contains useful functions (but
in object code form). If for example you use cout, then the
implementation of this will be in a library which your linker links in
with your own code to form an exe. A library can be considered
equivalent to an object file containing your own functions.
*** A header contains the correct signatures for functions which you
might call. Someone better help me out here :)

From what I'v gatherd a header is like a nametag for a library
(something like a subject of a e-mail message), and by refering to it am
actually calling a library.
** eg to use a vector you #include<vector>. The standard library
contains the implementation of a vector. The linker links the
required code into your exe if you use vector.

Hope I get a response to this :)
I hope this helps.

It does, thanks.
 
S

SG

[... I have no clue, please explain basic things to me ...]

Get hold of a (or more) decent C++ book(s). Check out
http://stackoverflow.com/questions/388242/

key words for your internet search:
separate compilation,
translation unit,
linker (computing),
one-definition rule

Header files are nothing special. They are simply best practice. Once
you understand that a C++ compiler likes to see declarations or
definitions of "things" before you can make "use" of them, and once
you understand separate compilation, it should be obvious what the
purpose of header files is.

SG
 
P

Paul

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?
No simple answer, object files comprise of symbol tables and relative
addresses , they are a format the linker can understand.
Types of common obj files are ELF and COFF, you may wish to study some
literature on these file formats to gain a better insight.

You can create non executable obj files as libraries of functions. Your
program can use these files, the best way to understand is to actually do
it, let me explain:

1) Create an object file containing one function and compile only( no link)
======== static_lib.cpp ==============
#include <iostream>

extern void output_line( char* p){
std::cout<< p << std::endl;
}
=================================
Compile with: cl /EHsc /c static_lib.cpp

Your compiler may take different commands this is for ms compiler and what
the commands mean:
cl = invoke compiler
/EHsc = enable C++ Exceptions( needed for iostream)
/c = compile only no link.
filename.cpp

2) Create a main program file:
========= test_prog.cpp =============
void output_line(char* ); //* need to declare the lib functions you use,
this is usually in a .h file supplied with your lib.*//

int main(){
char str[] = "This is the argument";
output_line( str); /*invoke the fucntion inside the obj file*/
}
===============================
Compile with: cl /EHsc test_prog.cpp /link static_lib.obj
Now you see the command takes a /link option which specifies an input file
for the linker.

obj files can be created using different langauges, usually asm obj files
are usefull. When assembled/compiled to an obj file it becomes portable
across languages. A large obj lib is usually a .lib file file these are used
extensivelly when compiling large windows programs( dunno about nix but
probably similar).
3. What is a function in the source code?
A function contains a defintion(usually) and a decalration.
..I think my above example of linking shows the difference between the two.

..>
4. What is an object in the source code?
An "object"? LOL you don't wanna go there.
5. What is a library, what does it contain?
There are different types of libraries.
answer1 ) Its a large building containing books (joke).
answer 2) See above link example for a static library.
answer 3) dynamic libraries are more advance I won't go into that.
6. What is a header file, what does it contain?
A header file contains declarations of the implementations is declares.
7. What is the connection between a library and a header file?
Again see my example of linking.
8. What is a namespace and what is it's connection between a library and a
header file?
A namespace is basically self explanitory , it basically adds a scope.
Example:
int x;
namespace newscope{int x;}
You can have two different varaible with the same names , because they are
in a different namespace/scope.
No namespace usually is the global namespace.
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.

--
HTH
Paul.
 
A

Aleksandar_B

Thanks for all the replies, this should get me on my way.


Windows 7 32bit
Ubuntu 10.04 32bit
 
B

Bart van Ingen Schenau

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
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top