5 UNIX / Linux gcc Command Examples

What is gcc?
5 gcc examples
Syntax and Options
Related Commands

What is gcc?

Gcc is the default GNU compiler. Its a very popular and powerful compiler used for compilation of C code.

5 gcc Examples

1. Basic compilation using gcc

For a basic compilation, gcc works as :

gcc helloworld.c

This command will produce an output file with default name a.out

2. Provide an output file name using -o

If a specific output file name is desired then this can be done using -o option.

gcc -Wall helloworld.c -o helloworld

The command above will produce an output file with name helloworld

3. Activate some basic warnings using -Wall

If some basic warnings (related to return type, typecasting etc) need to be activated then this can be done using the -Wall option.

gcc -Wall helloworld.c -o helloworld

The above command turns on a basic set of warnings while compilation.

4. Generate an object file using -c option

There are 4 stages in the compilation process. If intermediary output just before the linking stage is required then use the -c option.

gcc -Wall helloworld.c -c

The command above would produce a file named helloworld.o

5. Generate a preprocessed file using -E

If an intermediary output after the preprocessing stage is required then use the -E option.

gcc -Wall helloworld.c -E

The command above would produce a preprocessed output on stdout. The output can be redirected into a file to save it

Syntax and Options

 gcc [-c|-S|-E] [-std=standard]
           [-g] [-pg] [-Olevel]
           [-Wwarn...] [-pedantic]
           [-Idir...] [-Ldir...]
           [-Dmacro[=defn]...] [-Umacro]
           [-foption...] [-mmachine-option...]
           [-o outfile] [@file] infile...
Short Option Long Option Option Description
-c Compile or assemble the source files, but do not link. The linking stage simply is not done. The ultimate output is in the form of an object file for each source file.
-S Stop after the stage of compilation proper; do not assemble. The output is in the form of an assembler code file for each non-assembler input file specified.
-E Stop after the preprocessing stage; do not run the compiler proper. The output is in the form of preprocessed source code, which is sent to the standard output.
-v Print (on standard error output) the commands executed to run the stages of compilation. Also print the version number of the compiler driver program and of the preprocessor and the compiler proper.
-### Like -v except the commands are not executed and arguments are quoted unless they contain only alphanumeric characters or “./-_”. This is useful for shell scripts to capture the driver-generated command lines.
-pipe Use pipes rather than temporary files for communication between the various stages of compilation. This fails to work on some systems where the assembler is unable to read from a pipe; but the GNU assembler has no trouble.
–help Print (on the standard output) a description of the command line options understood by gcc. If the -v option is also specified then –help will also be passed on to the various processes invoked by gcc, so that they can display the command line options they accept. If the -Wextra option has also been specified (prior to the –help option), then command line options which have no documentation
associated with them will also be displayed.
–target-help Print (on the standard output) a description of target-specific command line options for each tool. For some targets extra target-specific information may also be printed.

Related Commands

gpl
gfdl
fsf-funding
cpp
gcov
as
ld
gdb
adb
dbx
sdb