You are on page 1of 3

Introduction to C++.

Learning the development tools:

The compiler:

Compiler is the software that translates the syntactically correct statements of a program into object
code or can also produce an executable using a linker and the object code produced. An object code
can't be run directly on a machine since it contains information about the object module in addition to
the machine instructions.
A linker is another program which is invoked upon compiled object files. A linker just identifies the
structure of the object files, resolves the functions and object linkages and creates an executable.

The Compilation Process:

The compiler has a preprocessor, its purpose is to parse and process the statements which are meant
written using the preprocessor directives. These directives include statements that begin with a #
(pound) sign. So the include statements , define statements ifdef and endif statements are processed by
the preprocessor.

When the preprocessor is working it is called the first pass. When this phase is successfully over, the
second pass starts. The purpose of the second pass is to check the syntax and generate the machine
code. While generating the machine code compiler also optimizes the code, optimization leads to
smaller and smart code.

• C++ enforces static type checking. For example a long variable can't be assigned a double
variable.
• C++ doesn't enforce dynamic type checking, that is type checking while program is running
because C++ was designed to produce high speed code just as C.
• Like C, C++ allows module programming so you can fragment your code into multiple files
each describing a useful module or a back end library which can then all be combined together
to produce a single executable file.

Function Declaration and Linkage:

A function declaration is an “announcement” to the compiler that this is how a function will look like.
For example.

Void myfunc(int);

Looking at this declaration see that the braces are missing and the statement is ended by a semicolon.
This tells the compiler that the function's body or the definition is someplace else but this is how it'll
look like so for example if a code calls this like shown below,

int main()
{
myfunc(2);
}
void myfunc(int a)//this is the definition
{
cout<<a;
}

As can be seen, the function's definition comes after the function has been called in main. This is
possible since the function was “declared” before it was called so the compiler knows that this is a
valid function and the definition would come at some point within the file. If you don't provide the
definition above then the compiler would give you an error saying that it couldn't find the function's
definition.

Linkage Explained:

The compiler uses the declaration to compile the C++ program while the linker uses the definition to
produce the executable. In the above example the function's definition is also in the same file as is its
declaration however when working on large projects it might be possible that someone else is working
on the definition while you only have the declaration. Clearly in that case the definition is present in a
different file than the one you are working on.

To resolve this issue, both C and C++ provide the extern identifier. When a function is declared using
the extern identifier it tells the compiler that the definition may not be present in the same file so it
looks in all the files for its definition which are used to create the executable.

To declare a function with external linkage just precede it with the keyword extern. So with external
linkage the above function's declaration would like like

extern void myfunc(int);

Including Headers:

A header file is a file that contains the declaration of the functions and other user defined data types and
variables that are available for use. Usually the implementation file is different from a header file but
this is NOT a requirement. For example a statement like #include<iostream> is an include directive
which includes a file called iostream. There's no restriction on the name of the header file. It may
have any extension and any valid name but traditionally the header files have had a .h extension but
really its not a requirement.

Why the <> brackets?

The compiler usually has a configuration file where it stores the default location of where to look for
header files and the libraries to link to. The <> brackets tells the compiler that it should look for the
file specified within the brackets in the default location and NOT anywhere else.

But obviously you would be creating header files of your own and would be including them. Since its a
tedious job to put those files in the default location as stored in the configuration file you can also
specify the compiler where to look for the header files in addition to the default location. You can do
this by specifying the file within quotes “”. So suppose that you are working in your CPP file is present
in the current directory while the header files are present in the directory headers then we can include
the file as...

#include “headers/myheaderfile.h”

The compiler first looks in headers folder which is present in the current directory if it can't find the file
then it looks in the <default location>/header that is in the folder header which is present in the default
location as specified in the compiler's configuration file.

You can thus also include a file present in the default location like,

#include “iostream”

without any error. However if you have a file named iostream in the current directory where your CPP
file is present then instead of the standard header your version of iostream would be included because
the order of search is that way when you use quotes.

Including C header files

Since a lot of code is present in the C standard C library, all of it was included in C++. The header files
however were changed to be con formant to C++, these headers were prefixed with letter c with the
extension removed. For example,

C header C++ header


String.h cstring
Stdlib.h cstdlib
Stdio.h cstdio

You might also like