|
|
|
Using makefile: One of the strengths of C++ is that it is not necessary for all modules of a program to be in the same file. However, this flexibility could become a problem during testing and debugging time if you had to determine all the module dependencies and type the commands to re-compile all the affected modules every time you corrected an error. To avoid this problem, you should use the make utilility provided by UNIX. The creation of a list of instructions which specify module dependencies and issue
commands for creation of object and executable files is the basis of the use of the make
utility. So, first, you need a bit of background information on the syntax of a A makefile has two distinct line formats: rules (or prerequisite rules) and commands. These two types of lines occur in pairs. A rule begins in the first position of the line, and has the following format: targetfilename: dependencyfilenames The name to the left of the colon is the name of target file, that is, the file that make should build. The list of one or more names to the right of the colon specifies the names of the files on which the targets are dependent, that is, names of files needed to create the target file. It is the dependency list which the make utility will use to determine whether a target file should be updated. Only if one or more of the files in the dependency list has been changed does the target file need to be recreated. The dependency rule is followed by a command line. A command line MUST begin with at least one tab character--not spaces. Otherwise, it will not be recognized as a command line. Otherwise, the format is the same as for the command you would use at the prompt to create the target. In your myc++ directory, you have the files complex.h, complex.cc, and main.cc. These three files comprise a program to manipulate complex numbers. The makefile for this program should be created using vi. (Note the tabs at the beginning of each command line.) % vi makefile Then enter the following text in the file: complex: main.o complex.o This file is constructed essentially from the bottom up. Consider the third dependency-command pair. In the first line of this pair, the dependency rule, you have specified that the file complex.o is dependent on complex.h and complex.cc. The command to create the file complex.o is on the command line, one which you will recognize right away as a compile command for C++. This same construction applies to the creation of main.o in the second dependency-command pair. The dependency rule of the first dependency-command pair specifies that a file named complex and will depend on main.o and complex.o, the files created by the other two pairs. The command line specifies the creation of the executable file complex. Now to use this file to actually create this executable file called complex, type % make complex (or just "make") To run the program, type % complex
Creating tar files for submitting a multi-file assignment: Some projects are comprised of more than one file. Since the submit program only allows you to send only one file, you will have to use a program called "tar" to combine all of files for a multi-file project into one larger one, called an archive. ("Tar" is an acronym for "Tape ARchiver", since it was originally used to make backup tapes.) To make your archive, the format you will use is tar -cvf <tarfile> <file list> for <tarfile> the name of the file you want to create and <file list> a list of file names, separated by spaces, that you want to hand in. To create a tar file named "compnum" for the complex number project you have been using in this tutorial, you should type % tar -cvf compnum main.cc complex.h complex.cc makefile Note that you are submitting the source files AND the makefile. These files will
usually be the requirement for an assignment If you want to check which files you included in the tar file, simple type % tar -tvf compnum |