-->
Previous Table of Contents Next


cproto

cproto is another program that is not included on this Linux CD-ROM but is readily available from FTP and BBS sites. cproto reads in C source files and automatically generates function prototypes for all of the functions. Using cproto saves you from having to type in a function definition for all of the functions that you have written in your programs.


Tip:  
To install cproto on your system, perform the following steps while you are logged in as root:
1.  Uncompress and untar the file.
2.  cd into the cproto subdirectory created as a result of the untar command.
3.  Move the file named cproto to the /usr/bin directory.
4.  Move the file named cproto.1 to the /usr/man/man1 directory.
5.  Remove the /tmp/cproto directory.

This installs the cproto program and man page on your system.


Run the following code through the cproto program:


#include <stdio.h>



main ()

{

  char my_string[] = “hello there”;

  my_print (my_string);

  my_print2(my_string);

}



my_print (char *string)

{

  printf (“The string is %s\n”, *string);

}



my_print2 (char *string)

{

  char *string2;

  int size, size2, i;



  size = strlen (string);

  size2 = size -1;

  string2 = (char *) malloc (size + 1);

  for (i = 0; i < size; i++)

    string2[size2 - i] = string[i];

  string2[size] = ’\0’;

  printf (“The string printed backward is %s\n”, string2);

}

The following output displays:


/* test.c */

int main(void);

int my_print(char *string);

int my_print2(char *string);

This output could be redirected to an include file and used to define the prototypes for all of the functions.

indent

The indent utility is another programming utility that is included with Linux. This program, in its simplest form, reformats or pretty prints your C code so that it is consistently indented and all opening and closing braces are represented consistently. There are numerous options that enable you to specify how you want indent to format your code. For information on these options, refer to the indent man page or type indent -h at the command line.

The following example shows the default output of the indent program.

C code before running indent:


#include <stdio.h>



main () {

      char my_string[] = “hello there”;

  my_print (my_string);

    my_print2(my_string); }



my_print (char *string)

{

  printf  (“The string is %s\n”, *string);

}



my_print2      (char *string) {

    char *string2;

      int size, size2, i;



      size = strlen (string);

      size2 = size -1;

      string2 = (char *) malloc (size + 1);

  for (i = 0; i < size; i++)

           string2[size2 - i] = string[i];

      string2[size] = ’\0’;

      printf (“The string printed backward is %s\n”, string2);

}

C code after running indent:


#include <stdio.h>



main ()

{

  char my_string[] = “hello there”;

  my_print (my_string);

  my_print2 (my_string);

}



my_print (char *string)

{

  printf (“The string is %s\n”, *string);

}



my_print2 (char *string)

{

  char *string2;

  int size, size2, i;



  size = strlen (string);

  size2 = size -1;

  string2 = (char *) malloc (size + 1);

  for (i = 0; i < size; i++)

    string2[size2 - i] = string[i];

  string2[size] = ’\0’;

  printf (“The string printed backward is %s\n”, string2);

}

Indent does not change how the code compiles; it just changes how the source code looks. It makes the code more readable, which is always a good thing.

gprof

gprof is a program that is installed in the /usr/bin directory on your Linux system. It allows you to profile programs that you write to determine where most of the execution time is being spent.

gprof will tell you how many times each function that your program uses is called and also the percentage of the total execution time the program spent in each function. This information can be very useful if you are trying to improve the performance of a program.

To use gprof on one of your programs, you must compile the program using the -pg gcc option. This causes the program to create a file called gmon.out each time it is executed. gprof uses the gmon.out file to generate the profile information.

After you run your program and it has created the gmon.out file, you can get its profile by entering the following command:


Tip:  
The profile data that gprof displays to the screen is quite large. If you want to examine this data, you should redirect gprof’s output to a file.

gprof <program_name>

The program_name parameter is the name of the program that created the gmon.out file.

f2c and p2c

f2c and p2c are two source code conversion programs. f2c converts FORTRAN code into C code, and p2c converts Pascal code into C code. Both are included in the Linux installation when you install GCC.

If you have some code that has been written using either FORTRAN or Pascal that you want to rewrite in C, f2c and p2c can prove to be very useful programs. Both programs produce C code that can typically be compiled directly by GCC without any human intervention.

If you are converting small, straightforward FORTRAN or Pascal programs, you should be able to get away with using f2c or p2c without any options. If you are converting very large programs consisting of many files, you will probably have to use some of the command-line options which are provided by the conversion program that you are using.


Tip:  
f2c requires that the program being converted has either a .f or a .F extension.

To invoke f2c on a FORTRAN program, enter the following command:


f2c my_fortranprog.f

To convert a Pascal program to C, enter the following command:


p2c my_pascalprogram.pas

Both of these commands create C source code files that have the same name as the original file, except with a .c extension instead of .f or .pas.

For more information on the specific conversion options that are available with f2c or p2c, refer to their respective man pages.


Previous Table of Contents Next