Wednesday, July 21, 2010

C++: Before we continue - viewing our coding results on the screen

Thanks to a comment from a reader, I've decided to write this small tip
before I continue with the next slide on conditional statements. The comment
says that I should write about compiling one's code to view it on a computer
screen, which is something I need to really get you going before I continue.

Before we view our results on a computer monitor, we need to perform what's
called "compiling." Compiling means translating a source code - the human
readable instructions to machine language so that a computer can work with
it. There are numerous processes involved, but the general breakdown is that
the source code is first translated into object files (a series of cryptic
messages and links). After this is done, a guy called linker comes up to
translate this object file into machine code. With the help of the
"librarian" or the standard template library, we get the final output - the
code that a computer can read and work with, called ironically, "machine

But there is a huge problem: just like we (humans) speak different
languages, various computers, or processors (in this case) knows its own
machine language - thus there are so many languages for different CPu
(Central Processing Unit) family. In order to help us resolve a possible
dispute, we (the programmers and engineers) use different compilers for
different computers. For instance, the most popular processors in the
computers today are X86 and its derivative, X64 (from Intel and others) and
ARM for mobile devices, with each of these processors using different
machine language. Even programs which were designed for older processors
have difficulty "fitting" in with the newer guys - especially if we want to
use our programs on different computers. But for now, our chief concern is
"getting our code to be read by a computer," so, the role of choosing the
compiler that best fits you is importnat to get going with our code.

In my case, I use Microsoft Visual Studio 2008, an IDE (Integrated
Development Environment) which allows me to write and read C++ code,
translate it into machine language (compile) and view the results on a
computer monitor. There are other compilers and IDE's out there, including
MinGW, GCC (for Linux) and Borland 9a bit old compiler). For our further
tutorials and notes, I'll mention VS2008 a lot, but general instructions
apply to other programs of this kind.

One more note before I continue: A popular saying says that many programmers
spend majority of their time debugging, or correcting their existing code
than write new ones. Debugging means, in terms of literal meaning, "remove
bugs." In our case, it means finding what went wrong with our program and
find ways of fixing, or improving our software. (here, a "bug' is a problem
with a program). I'll frequently use the terms "debugging" or "debug" to
note that we need to solve problems with our program before continuing,
especially as we go to later part of this C++ saga. (Just a note: compilers
have facilities to allow programmers to debug our code, such as Visual
Studio's Just in Time Debugger, GCC's debug switch and so forth..

Before i close this message, here are some keyboard shortcuts for Visual
studio users:
* To compile your code, press F7.
* To run your program, press F5.
* Press CTRL+TAB or CTRL+Shift+TAB to move between windows or source code
editor windows.

At least that's the end of the basics in C++. Starting from next post, I'll
discuss ways in which we can allow computers to have "some intelligence".
// JL
P.S. If you have any comments, please let me know.

No comments:

Post a Comment