Programming in C

(Barry) #1
Other Utilities for Working with Larger Programs 345

code or to re-create an older version for customer support, for example).With cvs
(which stands for Concurrent Versions System), you “check out” a program (using the
cvscommand with the checkout option), make your changes to it, and then “check it
back in” (using the cvscommand with the commitoption).This mechanism avoids the
potential conflict that can arise if more than one programmer wants to edit the same
source file.With cvs,programmers can be at multiple locations and can all work on the
same source code over a network.


Unix Utilities:ar,grep,sed, and so on


A wide assortment of commands available under Unix makes large program develop-
ment easier and more productive. For example, you can use arto create your own
library.This is useful, for example, if you create a bunch of utility functions that you fre-
quently use or want to share. Just as you linked your program with the –lmoption
whenever you used a routine from the standard math library, so too can you specify your
own library at link time, using the option –llib. During the link edit phase, the library
is automatically searched to locate functions that you reference from the library. Any
such functions are pulled from the library and linked together with your program.
Other commands such as grepand sedare useful for searching for strings in a file or
making global changes to a set of files. For example, combined with a little shell pro-
gramming skills, you can easily use sedto change all occurrences of one particular vari-
able name to another across a set of source files.The grepcommand simply searches a
file or files for a specified string.This is useful for locating a variable or function in a set
of source files, or a macro in a set of header files, for example. So the command


$ grep todaysDate main.c


can be used to search the file main.cfor all lines containing the string todaysDate.The
command


$ grep –n todaysDate .c .h


searches all source and header files in the current directory and displays each match pre-
ceded by its relative line number within the file (the use of the –noption).You have seen
how the C language supports division of your program into smaller modules and incre-
mental and independent compilation of those modules. Header files provide the “glue”
between your modules when you use them to specify shared prototype declarations,
macros, structure definitions, enumerations, and so on.
If you are using an IDE, managing multiple modules in a program is straightforward.
The IDE application keeps track of the files that need to be recompiled when you make
changes. If you’re instead using a command-line compiler, like gcc,you either have to
keep track of the files that need to be recompiled yourself, or you should resort to a tool
such as maketo automatically keep track for you. If you are compiling from the com-
mand line, you’ll want to look into other tools that can help you search your source files,
make global changes to them, and create and maintain program libraries.

Free download pdf