Appendix C. Compiling Programs with gcc

THIS APPENDIX SUMMARIZES SOME OF THE more commonly used gcc options. For information about all command-line options, under Unix, type the command man gcc. You can also visit the gcc Web site, http://gcc.gnu.org/onlinedocs, for complete online documentation.

This appendix summarizes the command-line options available in gcc, release 3.3, and does not cover extensions added by other vendors, such as Apple Computer, Inc.

General Command Format

The general format of the gcc command is

gcc [options] file  [file ...]

Items enclosed in square brackets are optional.

Each file in the list is compiled by the gcc compiler. Normally, this involves preprocessing, compiling, assembling, and linking. Command-line options can be used to alter this sequence.

The suffix of each input file determines the way the file is interpreted. This can be overridden with the –x command-line option (consult the gcc documentation). Table C.1 contains a list of common suffixes.

Table C.1. Common Source File Suffixes

Suffix

Meaning

.c

C language source file

.cc, .cpp

C++ language source file

.h

Header file

.m

Objective-C source file

.pl

Perl source file

.o

Object (precompiled file)

Command-Line Options

Table C.2 contains a list of common options used for compiling C programs.

Table C.2. Commonly Used gcc Options

Option

Meaning

Example

--help

Displays summary of common command-line options.

gcc --help

-c

Does not link the files, saves the object files using .o for the suffix of each object file.

gcc –c
enumerator.c

-dumpversion

-g

Displays current version of gcc.

Includes debugging information, typically for use with gdb (use –ggdb if multiple debuggers are supported).

gcc -dumpversion

gcc –g testprog.c
–o testprog

-D id

-D id=value

In the first case, defines the identifier

id to the preprocessor with value 1. In the second case, defines the identifier id and sets its value to value.

gcc –D DEBUG=3

test.c

-E

Just preprocesses files and writes results to standard output; useful for examining the results of preprocessing.

gcc –E
enumerator.c

-I dir

Adds directory dir to the list of directories to be searched for header files; this directory is searched before other standard directories.

gcc –I /users/
steve/include x.c

-llibrary

Resolves library references against the file specified by library. This option should be specified after the files that need functions from the library. The linker searches standard places (see the –L option) for a file named liblibrary.a.

gcc
mathfuncs.c -lm

-L dir

Adds directory dir to the list of directories to be searched for library files. This directory is searched first before other standard directories.

gcc –L /users/
steve/lib x.c

-o execfile

Places the executable file in the file named execfile.

gcc dbtest.c –o
dbtest

-Olevel

Optimizes the code for execution speed according to the level specified by level, which can be 1, 2, or 3. If no level is specified, as in –O, then 1 is the default. Larger numbers indicate higher levels of optimization, and might result in longer compilation times and reduced debugging capability when using a debugger like gdb.

gcc –O3 m1.c m2.c
–o mathfuncs

-std=standard

Specifies the standard for C files.[1] Use c99 for ANSI C99 without the GNU extensions.

gcc –std=c99
mod1.c mod2.c

-Wwarning

Turns on warning messages specified by warning. Useful options are all, to get optional warnings that might be helpful for most programs, and error, which turns all warnings into errors, thereby forcing you to correct them.

gcc –Werror
mod1.c mod2.c

[1] The current default is gnu89 for ANSI C90 plus GNU extensions. Will be changed to gnu99 (for ANSI C99 plus GNU extensions) once all C99 features are implemented.

 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.5.57