Previous Table of Contents Next


C++, ANSI C++, Windows, and Other Areas of Confusion

C++ is a language. DOS, Windows, UNIX, and MacOS are operating systems. When you learn C++ you’ll want to learn it as a portable language without regard to which machine and operating system you’ll run your programs on.

Teach Yourself C++ in 24 Hours makes no assumptions about your operating system. This book teaches ANSI C++. ANSI C++ is just another way of saying “standard” C++—the internationally agreed-upon version that is portable to any platform and any development environment.

Therefore, you won’t see anything in this book about windows, list boxes, graphics, and so forth. All that is operating system dependent. You’ll see output accomplished through standard output.

To make this work, you’ll need to tell your compiler to create a console application. Some compilers, written to be used with Windows or the Mac or another windowing environment, call this a quick window, or a simple window, or perhaps a console window.

Your Compiler and Editor

Your compiler can have its own built-in text editor, or you can use a commercial text editor or word processor that can produce text files. The important thing is that whatever you write your program in, it must save simple, plain-text files with no word processing commands embedded in the text. Examples of safe editors include the Windows Notepad, the DOS Edit command, Brief, Epsilon, EMACS, and vi. Many commercial word processors, such as WordPerfect, Word, and dozens of others, also offer a method for saving simple text files.

The files you create with your editor are called source files, and for C++ they typically are named with the extension .cpp, .cp, or .c. In this book, all the source code files are named with the .cpp extension, but check your compiler for what it needs.

Most C++ compilers don’t care what extension you give your source code, but if you don’t specify otherwise many will use .cpp by default.

Do Don’t
DO use a simple text editor to create your source code, or use the built-in editor that comes with your compiler. DON’T use a word processor that saves special formatting characters. If you do use a word processor, save the file as ASCII Text.
DO save your files with the .c, .cp, or .cpp extension.
DO check your documentation for specifics about your compiler and linker to ensure that you know how to compile and link your programs.

Compiling and Linking the Source Code

Although the source code in your file is somewhat cryptic, and anyone who doesn’t know C++ will struggle to understand what it is for, it is still in what we call human-readable form. Your source code file is not a program, and it can’t be executed, or run, as a program can.

To turn your source code into a program, a compiler is used. How you invoke your compiler and how you tell it where to find your source code will vary from compiler to compiler; check your documentation.

If you compile the source code from the operating system’s command line, you should type the following:
For the Borland C++ compiler bc <filename>
For the Borland C++ for Windows compiler bcc <filename>
For the Borland Turbo C++ compiler tc <filename>
For the Microsoft compilers cl <filename>
For the DJGPP compiler gxx <filename> -o <output executable name>

Compiling in an Integrated Development Environment

Most modern compilers provide an integrated development environment. In such an environment, you typically choose Build or Compile from a menu, or there can be a function key you press to build your application. Again, you’ll want to check the documentation for your particular compiler.

Linking Your Program

After your source code is compiled, an object file is produced. This file is often named with the extension .obj. This is still not an executable program, however. To turn this into an executable program, you must run your linker.

C++ programs are typically created by linking together one or more OBJ files with one or more libraries. A library is a collection of linkable files that you created, were supplied with your compiler, or that you purchased separately. All C++ compilers come with a library of useful functions (or procedures) and classes that you can include in your program. A function is a block of code that performs a service, such as adding two numbers or printing to the screen. A class is a collection of data and related functions; I’ll be talking about classes a lot.

The steps to create an executable file are:

1.  Create a source code file, with a .cpp extension.
2.  Compile the source code into a file with the .obj extension.
3.  Link your OBJ file with any needed libraries to produce an executable program.


Previous Table of Contents Next