Some useful things to remember about the gcc compiler

BACKGROUND: THE COMPILATION PROCESS:
Before getting into the options, a little background. When you compile a program as we've been doing, "gcc" checks the source code for errors and creates a binary object file of that code (if no errors exist). It then calls the linker to link your code's object file with other pre-compiled object files residing in libraries. These linked object binaries are saved as your newly compiled program. The options to "gcc" dictate the way in which this process is performed. For example, you could tell "gcc" to just create the object file and skip the linking, such as when developing large programs or building your own libraries -- how to do this is outside the scope of this class.

All of the options of the gcc command are explained in the "man" pages. Type "man gcc" at the Athena prompt and the (huge) list of options will be displayed.

OPTIONS IMPORTANT IN 10.001:
The gcc options that are important for 10.001 are as follows:

where Examples of the options "-I", and "-L" are given below in the description of "gcnr." The description gives an example of "-l", too. I explain "-o" and "-l" here.

-Wall
tells the compiler to implement 'all' Warning options. Warnings are diagnostic messages that report constructions which are not inherently erroneous but which are risky or suggest there may have been an error. Very useful for debugging code.
-ansi
tells the compiler to implement the ANSI language option. This turns off certain "features" of GCC which are incompatible with the ANSI standard.
-pedantic
used in conjunction with -ansi, this tells the compiler to be adhere strictly to the ANSI standard, rejecting any code which is not compliant.
-o
tells the compiler to save the compiled program under the name . So, typing "gcc myfile.c -o myfile.x" will take the source code of file "myfile.c" and create program "myfile.x" rather than the default program "a.out".
-l
tells the linker to search a standard list of directories for the library, which is actually a file named "lib.a". The linker then uses this file as if it had been specified precisely by name. The directories searched include several standard system directories plus any that you specify with "-L".
EXAMPLES FROM gcnr:
An example of using the above options is the little file named "gcnr", which we use to compile programs that use Numerical Recipes in C functions. The file "gcnr" is a one-line Unix script that issues the following command:

(Note that the above is a long command and may word wrap in your browser.) The numbers in this list mark each part of the command:

  1. /mit/cygnus/`machtype`bin/gcc
  2. -I/mit/recipes/src/recipes_c-ansi/include
  3. -L/mit/recipes/`machtype`lib
  4. $*
  5. -lrecipes_c-ansi
  6. -lm
  7. |& grep -v nrutil
where
  1. Tells Unix where to find the gcc program (`machtype` is needed to make sure that the correct version of gcc is used for the machine type you are using).
  2. Tells gcc that it should look in the Num. Rec. in C include directory to find nr.h and nrutil.h when it encounters #include "nr.h" and #include "nrutil.h" in your code.
  3. Tells gcc to look in the Num. Rec. in C library directory to find the pre-compiled binaries for all Num. Rec. in C functions used in your code.
  4. Is the Unix script notation for the command line argument to the script. In other words, when you type "gcnr myfile.c", "myfile.c" will be substituted in place of "$*".
  5. Specifies the name of the library of pre-compiled binaries for all Num. Rec. in C functions. It resides in the directory mentioned under (4). This option tells the linker to link in the Num. Rec. in C functions when building your program.
  6. Tells the linker to link in the math library. The name of the math library (i.e., file of pre-compiled binaries for all math functions) is "libm.a". (See explanation of "-l" above.)
  7. Pushes the output through a parsing program called grep. This looks through the output for lines containing "nrutil" and ignores them. This removes a number of error messages that do not prevent the executable from functioning.
CONCLUSION:
There are MANY options to gcc which can be used to make programming easier and faster. These are only a few of the more common ones. As mentioned above, the man pages for gcc list them all.
last modified: