"I saw 'cout' being shifted 'Hello world!' times to the left and stopped right there..." | ||
--Steve Gonedes, 2000 |
The first program in a new language should always be "Hello world!".
1 2 // Hello World Application 3 #include <iostream.h>4 5 int main()
6 { 7 cout << "Hello world!\n";
8 return 0;
9 } 10
![]() | The |
![]() | The |
![]() | The |
![]() | The |
The source for this example is in
HelloWorld.cpp
.
You might think that this is the shortest C++ program that you can have - It is not! The shortest valid C++ program is:
main() { }
There is no need for libraries, as there is no input/output, there is no need to specify a return
type for the main()
function as it defaults to a return type of int and an
empty method is a valid method. This shows how flexible C++ is in assuming default states and values. This
is also one of C++'s greatest weaknesses!
"C++ : an octopus made by nailing extra legs onto a dog" | ||
--Unknown |
There are several C++ compilers that you can use for this course. Once the compiler is ANSI compliant there should be no issue in using it with this module as we just require a standard non-windowing compiler. A Unix/Linux compiler will also work. The current recommended compilers are Borland C++ compiler 5.5.1 and Bloodshed Dev C++.
The company blurb: The Borland C++ 5.5 Compiler is the high performance foundation and core technology of
Inprise/Borland's award-winning Borland C++Builder product line and is the basis for
Inprise/Borland's recently announced C++Builder(TM) 5 development system for Windows
95, 98, NT, Windows 2000 and Windows XP. The Borland C++ 5.5 Compiler and associated
command line tools, is now available for free download from the Borland Web Site at the
address
Borland Free Compiler. To make it easier and to avoid filling in the
forms I have made the compiler available locally here -
BorlandCppCompiler.exe
(8.52MB)
The free download includes Borland C++ Command Line Tools:
Borland C++ Compiler v5.5 (bcc32)
Borland Turbo Incremental Linker (tlink32)
Borland Resource Compiler / Binder (brc32, brcc32)
C++ Win32 Preprocessor (cpp32)
ANSI/OEM character set file conversion utility (fconvert)
Import Definitions utility to provide information about DLLs (impdef)
Import Library utility to create import libraries from DLLs (implib)
Borland Turbo Dump to structurally analyse EXE, OBJ and LIB files (tdump)
Librarian for symbol case-conversion, creating extended libraries and modifying page size (tlib)
Included Libraries:
Borland C/C++ Runtime Library
ANSI/ISO Standard Template Library (STL)
Here are the steps to install the Borland C++ compiler on your PC.
Download the installation from here -
BorlandCppCompiler.exe
(8.52MB). You will
receive a warning line in Figure 2.1, “Borland Compiler Download Dialog Warning”. Choose "Save" and save it
to a temporary directory on your machine.
Browse to the temporary file and execute it. It will install the Borland compiler
in a suitable directory. It will suggest c:\borland\bccx.x.x
. I
choose c:\program files\bcc551\
to keep my machine neat. So all points
from here assume that you have installed in the same directory as me. If you have not then
substitute your path in for mine.
Download the file
HelloWorld.cpp
to a directory such as
c:\Temp
. You can do this by right-clicking the link and choosing
"Save Target As..." and then entering c:\Temp\HelloWorld.cpp
Change your path environment variable to include the compiler executables
directory. In my case this is C:\Program Files\bcc551\Bin
. To do
this go to the "Control Panel" under your start menu and open up "System". Under
"System" there is an "Advanced" tab. Choose this tab and press "Environment
Variables". Double-click Path as in Figure 2.2, “Setting the Path environment variable.” and add in the
C:\Program Files\bcc551\Bin
with a ";" before to separate the path.
Next you must create a file in your bin directory (in my case
c:\program files\bcc551\bin
) called bcc32.cfg
that only contains:
-I"C:\Program Files\bcc551\include" -L"C:\Program Files\bcc551\lib;C:\Program Files\bcc551\lib\PSDK"
and another one called ilink32.cfg
that only contains:
-L"C:\Program Files\bcc551\lib;C:\Program Files\bcc551\lib\PSDK"
This sets up your compiler environment to always find the include files and library files. Please
be careful if you are using notepad to edit these files that you do not allow notepad to append
a .txt file extension to these files. You should either put " " around the filename, e.g.
"bcc32.cfg"
or under "save as" you should set the "save as type" to
"all files".
Finally check that your compiler works by typing Bcc32 HelloWorld.cpp
in the C:\Temp
directory as in Figure 2.3, “The Hello World Application in Action.”
and then execute it by typing HelloWorld in the same directory.
The Bloodshed Dev C++ compiler is available at: http://www.bloodshed.net/devcpp.html. It is is a full-featured Integrated Development Environment (IDE) for the C/C++ programming language. It uses Mingw port of GCC (GNU Compiler Collection) as its compiler. Dev-C++ can also be used in combination with Cygwin or any other GCC based compiler. Install version 5 from the: http://www.bloodshed.net/dev/devcpp.html page through Sourceforge. This compiler requires Windows 95 or higher.
The Codeblocks IDE is available at: http://www.codeblocks.org/. It is is a full-featured Integrated Development Environment (IDE) for the C/C++ programming language. It uses Mingw port of GCC (GNU Compiler Collection) as its compiler. There is a version of Codeblocks for Linux and Mac as well as for Windows.
[6] Preprocessor directives are orders for the preprocessor, not for the program itself. They must be specified in a single line of code and should not end with a semicolon.
© 2006
Dr. Derek Molloy
(DCU).