Advertising (This ad goes away for registered users. You can Login or Register)

Learning Programming With C++ (II)

Discuss about your favorite (gaming...or not) devices here. The most popular ones will end up getting their own categories
Programming discussions for your favorite Device
Forum rules
Forum rule Nº 15 is strictly enforced in this subforum.
Post Reply
User avatar
SHA-1
Posts: 26
Joined: Sat Jun 09, 2012 9:20 am
Location: In my house...

Learning Programming With C++ (II)

Post by SHA-1 » Mon Aug 13, 2012 12:50 pm

<<Prev | Next>>

Underneath the Hood of the Compiler


Now that you set up your enviroment, you are ready to program.

But , WAIT right there.

To understand programming we need to know what happens underneath the hood of the compiler as this gives us a better apsect of whats happing to the code and it might help in the future projects you budding programmers will certainly make.

Keep in mind that a computer can only understand binary and binary only.
Right now, I think most of you will be going "What's Binary ?", and then have a 10-minute google search to find out.
Well, I'll spare you the trouble and tell you anyway.

A computer is very dumb, as it can only understand a sequence of one's and zero's called machine code.
So a computer can't actually understand C++, C or any other language.

So this is why we need a compiler, to translate code from C++ to machine code
*Note: The same process happens in the C compiler GCC

Preprocessing

The preprocessor is a tool that takes a look at the code and see's any symbols like:

Code: Select all

#include
or

Code: Select all

#define
**These bits of code are preprocessor directives, and that they make they code easier to handle or include essential libraries to make the code work.

And if it does it replaces bits and bobs of the code until every thing is fine and dandy.
Its like a spell check but for the programming language.
***Note: The code has not started compiling


Compiling

As soon as the preprocessor is done with the source code, then the compiling begins.

The compiler converts the C++ code into Assembly, and creates several optimisations and to stop the program from consuming too much memory and make the program faster.

So the compiler creates .s files form the generated Assembly.

Assembling

Once the compiler has converted all the C++ code into assembly, the assembler (an assembly compiler) takes the assembly code and converts it into .o files called object files. This is basicly machine code, but it still has symbols (i.e cout) and has no executable structure so the OS won't be able to recognise it as a executable.

Linking

Once the object code is generated by the assembler, the linker removes all the symbols and links the extra library code needed.

The finished product ,The Executable!!!

Once the linking is finished, the machine code is changed accoring to the target executable which depends on the OS.

The two most common are:

-Microsofts Win32 EXE Excutable
-Executable and Linkable Format (ELF)(This format is used by most POSIX systems and all the Playstation consoles) :geek:

Now after this insight are you a little bit more knowledgeable about what goes on underneath the hood? ;)

<<Prev | Next>>
Advertising
Last edited by SHA-1 on Wed Aug 15, 2012 10:40 am, edited 3 times in total.
Languages, I know:
C++, C, Perl, Bash.

Languages I'm learning:
Assembly, Python, Java.

Sirius
Posts: 103
Joined: Sat Dec 18, 2010 3:31 pm

Re: Learning Programming With C++ (II)

Post by Sirius » Tue Aug 14, 2012 6:38 am

nice tuto, I hope you can keep doing this tuto :)
Advertising

JJS
Big Beholder
Posts: 1416
Joined: Mon Sep 27, 2010 2:18 pm
Contact:

Re: Learning Programming With C++ (II)

Post by JJS » Tue Aug 14, 2012 8:30 am

I don't quite agree with your description of the preprocessor. It does actually not check the syntax of the program and merely performs replacement on special tokens. It has no knowledge of the underlying language and will happily produce syntactically incorrect results.

User avatar
SHA-1
Posts: 26
Joined: Sat Jun 09, 2012 9:20 am
Location: In my house...

Re: Learning Programming With C++ (II)

Post by SHA-1 » Tue Aug 14, 2012 10:22 am

JJS wrote:I don't quite agree with your description of the preprocessor. It does actually not check the syntax of the program and merely performs replacement on special tokens. It has no knowledge of the underlying language and will happily produce syntactically incorrect results.
After reading through my post again, I saw what you were talking about :oops: , so I corrected it!
Languages, I know:
C++, C, Perl, Bash.

Languages I'm learning:
Assembly, Python, Java.

Post Reply

Return to “Programming and Security”