[Courses] Some gcc options

Mary mary-linuxchix at puzzling.org
Tue Jul 16 00:26:07 EST 2002


The GNU Compiler Collection (gcc) is the most common C compiler found on
Linux systems. It is a command line compiler. These examples assume that
you are in the directory where your source file (I have called it
source.c here, just name them something.c)

The simplest gcc command:

        gcc source.c

This produces a file in the current directory called a.out, which is
executable. You can run it with the command "./a.out" (./ means look in
the current directory rather than $PATH)

Some flags you should use:

        gcc -Wall -pedantic source.c

These turn on extra warnings.

These warnings are things that gcc considers compilable, but abnormal.
For example, a memory address[1], which you store in a pointer, is just
an integer. But if you try and set an integer equal to the memory
address stored in a pointer, gcc picks up that you probably aren't
storing a *value* in the int, but an *address of a value*, or the
address where the value is kept, and will warn you.

Now, supposing you want to produce a file that is not called "a.out",
you would do something like

        gcc -Wall -pedantic -o myProgram source.c

and then you can execute "./myProgram"

Quick Warning: There is often already a executable on your system called
"test". By default it prints no output. While you could still call your
output file "test" and execute it with "./test", many people have
wondered why their test file produces no output when run!

Quick Warning #2: The name directly after the -o is where gcc will write
to. I once destroyed several hours work by typing "gcc -Wall -pedantic
-o source.c myProgram" - which compiled over the top of source.c !

If you want to compile several C files into one program:

        gcc -Wall -pedantic [-o myProgram] source1.c source2.c source3.c

etc...

-Mary.

[1] Each location in memory has a unique address, which is just a
number.



More information about the Courses mailing list