Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Advanced C 1992

.pdf
Скачиваний:
93
Добавлен:
17.08.2013
Размер:
4.28 Mб
Скачать

Part V • Appendixes

One downside is this compiler requires at least an 80386 CPU or faster. It cannot run on an 80286 or slower CPU. For professional developers, this may not be an issue, but for those of you who program as a hobby, it’s important to check whether the compiler runs under your hardware.

When managing large projects, a make facility is necessary. Microsoft’s compiler offers both the IDE’s make facility and a stand-alone make (NMAKE), both of which you can run under the DOS prompt. NMAKE accepts standard .MAK files (which can be written by hand); however, the IDE requires a make file it has created (because of its rather strict contents rules). You can take the IDE’s make file and use it with NMAKE without modifications.

When used under Windows, Microsoft C/C++ creates a program group like the one shown in Figure B.4. This group gives you access to each of the features that Microsoft’s C/C++ product offers to the Windows programmer.

Online help

 

 

 

 

 

 

 

 

 

DOS-based

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

IDE

Windows

 

 

 

 

 

 

 

 

 

 

 

debugger

 

 

 

 

 

 

 

 

 

WX Server

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

program

DOS

 

 

 

 

 

 

 

 

 

 

Readme

 

 

 

 

 

 

 

 

 

 

debugger

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

files

 

 

 

 

 

 

 

 

 

 

 

 

Figure B.4. Microsoft’s C/C++ Group under Windows.

When the IDE starts under Windows, you are presented with a character-based DOS application. This application enables you to do the following:

688

Compiler Variations

C C C

 

CBC

 

C C C

 

C

Define the files that make up the application.

Define the application’s attributes (such as whether it is a Windows or DOS application).

Edit the application’s files.

Compile and link the application.

Debug and run the application.

Various configuration options control the environment, compiler and linker options, and project options. Microsoft’s C/C++ IDE (under Windows) is shown in Figure B.5. Other powerful features include the extensive customizing ability of the IDE, including the ability to create functions and complex macros, which Borland’s IDE doesn’t enable you to do.

Figure B.5. Microsoft’s C/C++ IDE (in a Windows DOS Box).

Notice the similarity between the IDE under DOS and Borland’s IDE under DOS.

689

Part V • Appendixes

With a DOS-based IDE, the loss of a toolbar is still the main difference. Perhaps making up for this is the fact that for some computers, the DOS-based IDE is faster when the computer is running in a character video mode. Lack of a toolbar doesn’t present a major problem for most programmers because the functions in the toolbar are available as menu functions.

Like the Borland compilers, a shortcoming of the Microsoft compilers is they produce only 16-bit code. I hope Microsoft soon provides the 32-bit code generating version of this product because many C programmers will be developing 32-bit code in the near future.

Microsoft’s C/C++ compiler generally produces better and faster programs than many of its competitors, mostly because of Microsoft’s extensive compiler development experience. This program is a good choice if you don’t need a Windows-based IDE and you can live with the requirement for an 80386 or better operating environment.

QuickC for Windows 1.0

QuickC for Windows was introduced in 1991 by Microsoft as their first Windowshosted IDE product for C. This product had great potential; however, it has not kept its popularity.

QuickC for Windows has a rather straightforward IDE, as shown in Figure B.6. This IDE has an integrated debugger (not Codeview) that allows C source level debugging.

Again, as for other Windows-based IDEs, QuickC for Windows offers a toolbar to give quick access to features such as compile, compile and link, and access to some debugger functions.

This product has several advantages.

I’ve noticed (but others who have benchmarked QuickC for Windows don’t agree with my results) that QuickC for Windows is a fast compiler.

It is easy to use, with its projects easy to create and build.

It offers the ability to create DOS applications under Windows, something no other Windows-based IDE does.

It does optimizations (but not to the level that C/C++ 7.0 does). Because this compiler is similar to C 6, it probably could be effectively compared to C 6.

690

Compiler Variations

C C C

 

CBC

 

C C C

 

C

It offers, for an attractive price, an entry into Windows programming. It includes many of the features of the Windows SDK; most notably missing are the help compiler and the debugging version of Windows, though both can be purchased separately.

Figure B.6. Microsoft’s QuickC for Windows IDE.

The following section outlines some disadvantages of QuickC for Windows:

This product doesn’t interact well with Windows error trapping and debugging. QuickC for Windows can trap UAE’s, but it doesn’t provide any information to enable you to locate the error. It’s generally easier to bypass the QuickC for Windows IDE and run the errant program directly under Windows, let it UAE, and check the DrWatson log file to determine where the failure occurred.

Again, when using the debugging version of Windows (an option with QuickC for Windows), many of the usual errors Windows traps and makes known to the programmer sometimes go unreported. Also, OutputDebugString() doesn’t function when a program runs under QuickC for Windows IDE, making it difficult to write debugging information to the debugging terminal.

691

Part V • Appendixes

A final and most serious defect of QuickC for Windows is that it supports only Windows 3.0, but not the later versions such as Windows 3.1. Many QuickC for Windows users hope Microsoft will correct this problem, but Microsoft has not indicated it will upgrade this product soon. However, you can purchase the Windows SDK, which is compatible with QuickC for Windows.

In all, QuickC for Windows is a good way to get started writing Windows programs, but if you are developing software professionally, you may find QuickC for Windows too restricted.

Watcom C/386 9.01

If Microsoft’s QuickC for Windows and Borland’s Turbo C++ for Windows are cars…and Microsoft’s C/C++ 7.0 and Borland’s C++ 3.1 are pickup trucks…then Watcom’s C/386 9.01 compiler is a dump truck—a big dump truck.

Give this compiler a job and it does it. Watcom’s C/386 might easily be described as “for professional use only.”

The most important feature missing in Watcom’s package is the IDE. You must provide your own source editor to create the source code files. With so many capable source editors available, this is not a problem. You create the make files by hand. Watcom includes an effective make facility for larger programs.

The most important aspect of Watcom C/386 is that it is a 32-bit compiler, unlike the other compilers I’ve discussed. This means you can create true 32-bit applications (which then require an 80386 or better to run) that take full advantage of the more powerful 32-bit CPUs.

This product produces DOS applications (32-bit protected mode), Windows programs (again, 32-bit, with a special library of 32-bit Windows functions), and OS/2 V2.0 programs. It also produces AutoCAD ADS and ADI compatible code.

Watcom C/386 offers options that take advantage of some of the 80486’s better instructions. Contrary to popular belief, the 80486 is not an 80386 with a builtin math coprocessor, but an improved CPU, offering better instruction timings, a built-in cache, and other features that the compiler can use.

692

Compiler Variations

C C C

 

CBC

 

C C C

 

C

Some of the Watcom C/386’s advantages include:

Generates highly optimized 32-bit 80386/80486 code. This code allows an application to support a “flat” model program up to 4,000M (4 gigabytes).

Is both ANSI C and IBM SAA compatible. Its compatibility with Microsoft C makes it easy to port applications written with Microsoft C.

Supports Windows 3.x, DOS, OS/2 V2.0, and AutoCAD ADS and ADI.

Gives optimizations for the 80486 processor.

Watcom C/386 includes a 32-bit protected mode DOS Extender. This product, from Rational Systems, has a royalty-free license. It also supports up to 32M of virtual memory.

The debugger works within large 32-bit applications, using a number of different techniques.

The compiler comes with a high performance 32-bit linker.

The compiler executes under both DOS and OS/2.

The compiler supports inline assembly.

Source code profiler assists in code optimization.

Graphics library supports EGA, VGA, and Super VGA modes.

Includes support for PenPoint.

Under OS/2, Watcom C/386 integrates with WorkFrame/2 to provide a solid development environment.

OS/2 applications can access up to 512M virtual memory.

Windows programs can be fully 32-bit. You don’t need to develop part of the program as a 16-bit application and part as a 32-bit.

Watcom C/386 includes the Microsoft Windows SDK components.

Creates 32-bit DLLs with Watcom C/386. These DLLs are easier to create because you don’t need to consider the issues of segments.

Watcom C/386 offers probably the most extensive optimization possible.

693

Part V • Appendixes

Watcom C/386 provides excellent error checking, including checks for questionable type matches, uninitialized variables, unreferenced variables, questionable operations, and potentially incorrect statements.

Additionally, the Watcom C/386 includes the following utilities:

A linker that supports 32-bit executable programs; this linker runs interactively or in a command-line mode. The linker supports debugging symbolic information (such as line numbers and variable names). There is also a protected mode linker that enables you to link large programs.

A make utility that is basically compatible with UNIX-type make programs, such as Microsoft’s NMAKE program.

A source profiler program that helps determine which parts of the program consume the most CPU time.

An object code librarian that creates .LIB files.

A bind utility that creates 32-bit Windows programs.

A disassembler that can disassemble .OBJ files. This disassembler works with software created with Watcom C/386 and many other compilers and assemblers. This tool can be invaluable when debugging software at the machinecode level.

An object (.OBJ) file converter that converts .OBJ files to other (standard) formats, such as Microsoft format.

A comprehensive graphics library that is compatible with Microsoft’s graphics functions.

The full Windows SDK is not included. Programmers who wish to develop Windows programs will want the SDK for its tools and documentation. Acquisition of the SDK solves the problem of a lack of Windows development documentation. This situation is also true for OS/2 V2.0 software. You will want to use IBM’s development tools with Watcom C/386 when developing OS/2 applications, something for which this compiler is well suited.

Overall, Watcom C/386 is an advanced optimizing compiler that offers many tools, but has no IDE interface. The programmer must set up the project by hand, invoke the source editor directly, and compile (or build) and correct bugs, as programmers have done for years.

I hope Watcom soon offers an IDE for this compiler and thus effectively eliminates the C/386’s only shortcoming.

694

CIntroductionCto C++CC C C

CCC

C C C C

C C

C C C

Introduction to C++

What is C++?

C++ was created as a preprocessor for C and was originally called C with classes. This name proved to be too long and was shortened to C++. The relationship between C and C++ is easy to understand: C++ is a superset of the C language. This association makes it easier for a C programmer to become proficient in C++ programming; however, if you are not yet proficient in C, jumping into C++ would be difficult.

The most commonly used basic reference for C++ is The Annotated C++ Reference Manual by Margaret A. Ellis and Bjarne Stroustrup (Addison-Wesley, 1990).

The C++ ANSI standards committee, X3J16, has not yet defined an ANSI standard, but AT&T is setting the dominant standard. AT&T’s version 2.1 is the most commonly followed implementation of C++. Only a few compiler producers still use version 2.0. AT&T’s later standard, version 3.0, is beginning to be accepted and will soon be the most commonly implemented C++ standard.

695

Part V •Appendixes

Object-Oriented Programming (OOP)

C++ was designed from the outset to support object-oriented programming (OOP). To better understand OOP, you need to understand the concepts of abstraction, encapsulation, and hierarchies.

Abstraction

Abstraction is the capacity to ignore the details of an object. In this case, the object can be either a data object or a function.

When you write in a low-level language, you spend a lot of time working out the details (at the machine level) of a process or the exact representation (the ordering of bits and bytes) of data objects. With a higher-level language, you gain the advantage of fewer details for which the programmer is responsible. For example, a function written in assembly language might require thousands of lines of code to perform a simple task, while a function written in C might do the same task using only hundreds of lines. An even higher-level language might do the task with less than one hundred lines of code.

Data abstraction might enable a programmer to look at a floating-point data object that contains the value 3.1415926 without considering the likely hexadecimal or binary representations.

Encapsulation

Encapsulation is the process of making a class’s internal data parts invisible to the actual program. The only way to access or modify a class’s data is to use one of the class’s functions.

Limiting access to data offers two important benefits.

First, you don’t need to consider the internal representation of the data when accessing it. If the data is part of an array, you don’t need to question whether the access exceeds the bounds because the encapsulation functions can check for you.

696

Introduction to C++

C C C

 

CCC

 

C

C

C

Second, the program is much sturdier because only a limited number of functions actually modify the data, and all are located within a single, defined part of the program. If the data is modified incorrectly, the encapsulation layer must be improved with better error checking and correction.

If possible, encapsulate a class’s data. Doing so makes the application more reliable, and easier to modify and improve as the application grows.

Hierarchies

Our lives are categorized by hierarchies: our houses are organized in blocks; those blocks are in neighborhoods; those neighborhoods are in towns; those towns are in counties; those counties are in states; and so on.

In programming, organizing objects into a hierarchy simplifies management of the objects. For example, your database might have fields for a dozen variables. It is easier to write these variables to a disk file as a single object rather than as a dozen separate writes, one for each field.

Learning C++

The best way to learn C++ is to first learn C, and then write a C++ program with most of the program in C and only a few lines in C++. Listing C.1, HELLOCPP.CPP, is written this way. Also notice the new filename extension, .CPP—shorthand for C++. .C++ isn’t a valid filename extension on many computer systems.

Listing C.1. HELLOCPP.CPP—A first C++ program.

#include <iostream.h>

// Stock C++ hello program void main()

{

cout << “Hello world, this is C++\n”;

}

697