Wednesday, November 25, 2009

GCC options you should know



GCC options you should know


By Anurag Phadke <cbca@mantraonline.com>


Posted: ( 2001-01-09 10:22:13 EST by )



Using the GNU Compiler Collection (GCC) isn't for the faint of heart or
those used to the friendlier Windows IDEs. There are a huge number of
command line options available for GCC. This article brings you the
most commonly used and most useful options.

Writing a thousand line code is better than having to debug it.
The mistakes can vary from a missing ";" error to major errors of
logic. At the end of the day, code that refuses to run is merely a temp
file waiting to be deleted!

Given
below are the most often used gcc options. Though a lot more exist,
these are the most commonly used ones (26 to be specific) and can make
debugging easier.

For more information refer to the man pages
type "man gcc" at the command line. GCC offers a host of options, some
even for the AMD-K62 processor.

The common syntax is gcc [option]
[filename]. All options listed below are case sensitive. The option -v
differs completely from -V . Also options may be put together in pairs.
So, -vc is different from -v -c.

1) Extensions do matter: The extension given to a file determines its type and the way it should be compiled.

.c => c source file - preprocess, combine and assemble.
.m => objective-c source file - preprocess, combine and assemble.
.i => pre-processed C file - compile and assemble.

2)
-x language [c, c++, objective-c etc.] - Compiles the file with respect
to the given option. A file with '.m' extension can be compiled as a
'.c' extension file using the command "gcc -x c filename.m". If no
language is specified the file is compiled depending upon the extension.

3) -c : Compiles without linking. All successfully compiled binaries are saved as "filename.o".

4)
-S : Stop after compilation and generate the assembly code. This code
will be saved in a file with the extension ".s". A extract of the
assembler code is given below.

main:
pushl %ebp
movl %esp,%ebp
subl $8,%esp
addl $-12,%esp
pushl $.LC0

5)
-o filename: The compiled binary is saved with specified filename. If
not specified, the default is the save the binary as a file named a.out
in the directory.

6) -v : Provides the release number of your
compiler. Useful if one is getting an unexplainable error. The compiler
may be a old version.

7) -pipe : For a big program involving a
lot of inter linked files and functions, temporary files tend to get
created while compilation. When this option is specified, the compiler
makes use of pipes rather than creating temporary files. The output of
each function is piped to the next. The swap space is used instead.

8)
-ansi : Turns off features of gnu C which are incompatible with ANSI C.
This does not put a complete ban on non-ANSI programs. Certain Turbo C
features will work. A "-pedantic" option is required to force strict
conformance to the ANSI standard.

9) -ffreestanding : Compiles
in a freestanding environment. This makes use of the '-fno-builtin'
option along with main having no special requirement.

10)
-funsigned-char: Declares character S as unsigned. Since character is
machine specific, this serves to avoid confusion and common program
errors when run on various platforms. For example, suppose a Windows
compiler takes a defined character [syntax : char a;] as signed, while
the Linux takes it as unsigned. Now, suppose you are having a "if
-else" statement -- if (a==0) { /*statements }. Different compilers
have different defaults for the signedness of char. Specifying whether
a char should be signed or unsigned, removes ambiguity. For example, on
the Windows compiler, this statement will be executed considering
character as signed while on Linux they are executed considering
character as unsigned. This shall result in dubious results because
Windows will interpret the object differently. "-fsigned-char" declares
a character as signed.

Pre-processor options

1.)
-E : Do only preprocessing, no compiling or assembling. Output results
to standard output file a.out. Some of the options like -imacros work
in tandem with -E and require -E to be present along with it.

2.)
-include file : Compiles the file first. The options such as -D, -U are
compiled first, regardless of the order in which they are listed. The
-include and -imacros get compiled in accordance to the way they appear
at the command line.

3.) -imacros : Compiles macros irrespective
of the output. The only aim here is to compile the macro and make it
available to the program.

4.) -C : Do not discard comments (used with -E option)

5.) -P : Do not generate "#line" commands (used with -E option)

6.) -M : Describes the dependencies of each object file.

Linker options

1.)
-laddition : Use library bearing the name addition. Searches a list of
standard directories for the library named addition. A "-L
/place/where/library/exists" searches the directory specified.

Directory options

1.) -Idirectory : Append a list of directories to the standard directory list.

2.)
-I- : Specifying -I after -I- searches for #include directories. If -I-
is specified before, it searches for #include "file" and not #include .

Warning messages

1.) -w : Inhibit all warning messages.

2.)
-W : Print extra warning messages. For example, a function not
returning a value, a defined variable not being used. The warning
messages issued over here do not imply that the program has not
compiled successfully. It just helps to make a perfect program. Code
can be cleaned up as you will be notified of unused variables.

3.)
-Wswitch : Warn when 'switch' command is used. There are more options
here. The man page is the best resource for these additional switches.

Debugging options (Kill those bugs)

1.)
-g : Produce extra debugging information in the native O.S. format.
This information is of use to only the GNU Debugger (GDB).

-gstab gives debugging information in stab format.

2.) -gdwarflevel : Request debugging information along with level. Default level is set to 2.

Level 1 : Minimal amount of code to backtrack.
Level 3 : Maximum amount of code to backtrack.

3.)
-save-temps : Store the temporary generated files, permanently in the
current directory. The files are named on basis of the source file.

4.) -O (0 or 1 or 2 or 3)

0: Do not optimize.
1: Optimize. Requires a little more time and a lot of memory.
2 and 3 : Memory hogs but essential when dealing with large functions.






Other articles by Anurag Phadke

No comments: