/ Appendix D 771
Appendix D Command Line Compiling and Linking
Many C++ compilers provide an easy-to-use interface, including an editor (allowing you to create or edit programs) with simple buttons that you click to compile and run your programs. Other C++ compilers provide a “command line” mode. With a command line compiler, you use your favorite editor program to create or edit programs, and after a program is created you type one or more com- mands to compile, link, and run the program. This appendix provides the details of compiling, linking, and running programs for two popular command line compilers: • The GNU Project C++ Compiler, produced by Free Software Foundation, Inc., and widely available on Unix®, Linux®, and Microsoft Windows® machines. Appendix K provides instructions for obtaining the GNU compiler for Windows machines. • The Borland® C++ Compiler, produced by Inprise, Inc., for IBM-compatible PCs running DOS or Windows. With this compiler, you may prefer to use the interactive interface, but you may also use it as a command line compiler, typing your command at the DOS prompt.
Compiling Both command line compilers can compile any “.cxx” file on their own. The act of compiling cre- ates an object file, which contains low-level machine instructions corresponding to your C++ code. By the way, these machine instructions are usually specific for a given type of machine. For exam- ple, you cannot create an object file for an IBM-compatible PC and expect the object file to work on a Sun workstation. Anyway, the general command to compile a “.cxx” file with the GNU and Borland command line compilers are shown here: Compiling with GNU Compiling with Borland g++ -Wall -c filename.cxx bcc-w-c -P filename.cxx
In both commands, the filename.cxx may be any “.cxx” file that you want to compile. The minus signs in the commands provide various options. For example, -Wall (in GNU) and -w (in Borland) ask the compiler to provide all possible warning messages. The -c option specifies “com- pilation only” (rather than combining compiling and linking). In the Borland command, the -P option indicates that the file contains C++ code (rather than mere C code). For the Borland com- piler, a file extension of “.cpp” is assumed to have C++ code, but other extensions, such as “.cxx”, require the -P option. The GNU command will compile filename.cxx , creating an object file named filename.o . The Borland command will compile filename.cxx , creating an object file named filename.obj. A program that is split between several “.cxx” files can be compiled with a single command, placing all the file names at the end of the command line. Or, you may give separate commands to 772 / Command Line Compiling and Linking compile each piece of the program. For example, consider the demonstration program demo2.cxx from Figure2.8 on page61. This program makes use of throttle.cxx, so we must create two object files with the commands shown here: Compiling with GNU Compiling with Borland g++ -Wall -c throttle.cxx bcc-w-c -Pthrottle.cxx g++ -Wall -c demo2.cxx bcc-w-c -Pdemo2.cxx
If you are using an older compiler that does not support the bool data type, then these command lines need a modification. We’ll discuss this modification in detail in ; for now you should know that these highlighted options must be added in order to provide simple boolean values: Compiling with GNU compilers that do not have the bool type g++ -Wall -c -Dbool=int-Dfalse=0-Dtrue=1 throttle.cxx g++ -Wall -c -Dbool=int-Dfalse=0-Dtrue=1 demo2.cxx
Compiling with Borland compilers that do not have the bool type bcc-w-c -P -Dbool=int-Dfalse=0-Dtrue=1 throttle.cxx bcc-w-c -P -Dbool=int-Dfalse=0-Dtrue=1 demo2.cxx
With the GNU compiler, these commands produce two object files, throttle.o and demo2.o. The Borland compiler produces object files throttle.obj and demo2.obj. Once the object files are present, you can move to the next step: linking the pieces together.
Linking Before you can run a program, you must link the different object files together. The general form of the linking commands for GNU and Borland are shown here with an example: Linking object files with GNU g++ file1.o file2.o... -o filename
Example: g++demo2.othrottle.o-odemo2
Linking object files with Borland bcc file1.obj file2.obj...
Example: bccdemo2.objthrottle.obj The result of these commands is an executable file that can be run on your machine. In the case of the GNU compiler, the name of the executable file is explicitly given by the -o filename option at the end of the command. In the example, we have called the executable file demo2, so after the GNU linking step you would find a file named demo2 (with no file extension). If you forget the -o option, then GNU places the executable in a file with the peculiar name “a.out”. / Command Line Compiling and Linking 773
The name of the executable file does not need to be specified for the Borland compiler. This com- piler automatically uses the name of the first object file, changing the file extension to “.exe”. In the example given on the previous page, the executable file created by the Borland compiler will be demo2.exe.
Running a Program Once the executable file has been created, it can be run by typing the file name (without any exten- sion). In both of the examples shown above, you can run the demo2 program by typing the com- mand:
demo2
This will run the demo2 program with standard input from the keyboard and standard output to the monitor. You can also run a program with standard input taken from a file (rather than the keyboard) or run the program with standard output sent to a file (rather than the monitor). To redirect input, use the option “
demo2results
Linking with the math.h Library If you use the math.h library (or
Example if the demo2 program used the math library g++demo2.othrottle.o-lm -odemo2
The -lm option stands for “link math” (so make sure that you use the letter “l” rather than the digit one “1”).
Specifying an Include Directory Both the GNU and Borland compilers allow you to specify a particular directory where #include files can be found. The option is specified by -I directory . For example, we keep many of our header files in a directory named c:\source\headers, so with the Borland compiler we have:
bcc -Ic:\source\headers -w -c -P demo2.cxx 774 / Appendix E
For additional information on older versions Appendix E of Microsoft Visual C++, please see Dealing with Older Compilers www.cs.colorado.edu/~main/vccode.html
The boolean data type, static member constants, namespaces, the new C++ header file names, the typename keyword, and new Standard Library features (such as iterators) are not supported by older compilers. This appendix has suggestions for handling these features in older C++ compilers.
If Your Compiler Does Not Have the bool Data Type The int data type can simulate the new bool data type. The simulation is most easily carried out by placing these three definitions in your own header file called bool.h:
#define bool int #define false 0 #define true 1
Any program that uses the bool type should include your bool.h. If you are using a command line compiler, such as the GNU compiler or the Borland® compiler described in , then you may incorporate the three definitions directly in your compilation command. For example, using the GNU compiler to compile throttle.cxx, you would use the highlighted definitions shown here:
g++ -Wall -c -Dbool=int-Dfalse=0-Dtrue=1 throttle.cxx
If Your Compiler Does Not Have Static Member Constants We have made extensive use of static member constants, particularly in container classes. For example, our first bag from Chapter 3 had the static member constant CAPACITY to determine the maximum number of elements that can be placed in a bag:
class bag { public: // TYPEDEF and MEMBER CONSTANTS static const size_type CAPACITY = 30; ...
The complete rules for writing static member constants are given on page104. But what should you do if your compiler does not permit static member constants? There are two common solutions:
1. You may use an ordinary constant declaration, prior to the class definition. In the case of the bag we would write:
const size_t BAG_CAPACITY = 30; class bag { ... / Dealing with Older Compilers 775
2. For integer values, you may use an enum definition within a class definition. An enum defini- tion is allowed to assign integer values to a whole set of identifiers, but you can use it to cre- ate a name for just a single integer value. In our bag example, we would write:
class bag { public: enum { CAPACITY = 30 }; ...
The keyword enum appears before the list of values that you want to define, and the list itself is enclosed in curly brackets. After this definition, the name bag::CAPACITY is defined to have the integer value 30. Although this is not the intended use of an enum definition, the result is the same as using a static member constant.
Namespaces Older compilers do not support namespaces. We don’t know of any good solutions to this problem, apart from not putting your classes into a namespace if your work is likely to be used on an older compiler. In addition, you will need to choose names of your classes and functions carefully so that conflicts are not created between your names and names chosen by others.
Include File Names On page11, we describe the differences between older header file names (such as iostream.h and stdlib.h) and newer names (such as iostream and cstdlib). Newer compilers may also use the older names (but in that case, the items in the header files are not part of the std namespace).
The Typename Keyword Sometimes a template class defines auxilliary types such as bag
The std::iterator Class Building your own iterators was not a big part of this text, although we did show two examples in Chapter 6, both of which made use of the std::iterator class. The precise format of the std::iterator class was one of the last things finalized in the C++ Standard, so many compilers do not support the standard form of std::iterator. The GNU 2.95.2 compiler supports the stan- dard iterator, but requires a bug fix to work properly. Our latest information about writing your own iterators (with or without std::iterator) is provided online at www.cs.colorado.edu/~main/iterators.html. 776 / Appendix K
Appendix K Downloading the GNU Compiler and Software
Many of our classes have used the GNU g++ compiler and related software from the Free Software Foundation. Although the compiler lacks a graphical interface, it does work well in combination with the emacs editor and the gdb debugger. In addition, our students can download the software to run on a typical Microsoft Windows® machine, and later find virtually the same programming environment on a Unix® or Linux® machine. Our current version of the software comes primarily from the mingw32 GNU Windows tools, initially written by Colin Peters with significant modifications by Jan-Jaap van der Heijden and Mimit Khan. Other authors of our current tools include Geoff Voelker and Andrew Innes (ntemacs), Binu Jose Philip (dlgopen), Jean-loup Gailly (gunzip and tar), Steve Kirkendall (elvis), and Konstantine Knizhnik (winbgi). Overall, the tools include • The g++ compiler for C++ • The GNU gdb debugger • The GNU make facility • NT emacs editor • Elvis editor • Winbgim graphics library for the GNU compiler
To download and install the software, please follow the directions at:
www.cs.colorado.edu/~main/cs1300/README.html
Although we cannot provide technical support for this software, we have used it with over 1000 students with relatively few problems. The www.cs.colorado.edu/~main/cs1300/README.html page also contains links to some lab exercises that we use to introduce students to the compiler and related software.