gcc Hints and Tips for Beginners

Compiling with the gcc compiler

Preliminary notes

Using the compiler

The gcc command invokes a C compiler. In its most basic form, the compiler is invoked like this:
gcc sourcefile
where sourcefile contains C source code. This command compiles (and links) the source code to produce an executable file a.out, which you can subsequently execute by typing its name as a command. (Just typing a.out may work, and if not try ./a.out.)

The compiler also has many, many options. To compile with one or more of these options, invoke the command like this:

gcc [option1] [option2] .... sourcefile
Here are some of the most useful for beginning programmers: You can read all about these options and many more in the man page for gcc (which you can read with the command man gcc).

A shortcut

I recommend that you compile your programs using the -Wall and -pedantic options, plus -std=c99 if you want to use C99 features. To avoid having to type all of that in for every program, you can do the following:
  1. Create a text file called Makefile, in the same directory with your source files, containing the single line:
    	CFLAGS= -Wall -pedantic -std=c99
    	
    (Use a text editor to create this file.)
  2. To compile program program.c, issue the command make program. (Note that you leave off the .c suffix.) This command automatically invokes the gcc compiler as if you had typed
    	gcc -Wall -pedantic -o program program.c
    	
    So you get the extra warnings, and the executable is called program rather than a.out
(The make command is a powerful and flexible tool whose capabilities are well beyond the scope of this guide, and even beyond its man page. If you are interested, consult a good book on UNIX, or equivalent online documentation, such as the GNU documentation for make.)