
/ 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 “<infile” when you run the program (where infile is the name of the file that provides the input). To redirect output, use the option “>outfile” when you run the program (where outfile is the name of the file where you want the output sent). For example, to run the demo2 program with input from a file called data and output to a file called results, you would give the command: demo2<data>results Linking with the math.h Library If you use the math.h library (or <cmath>), some versions of the GNU compiler require -lm to be listed during the link step, after all the object files. For example, if the demo2 program used the math library, then we would link with the following example command: Linking object files with GNU (math library needed) g++ file1.o file2.o ... -lm -o filename 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.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages6 Page
-
File Size-