1.4. Introducing GCC

Now we're going to show you a tiny bit of C code and how to compile it. The point here is to show you how to use GCC so we won't explain the C code yet. Here's the smallest C program that GCC will compile. It does nothing.

Example 1-2. tiny.c

main()
{
}
     

Type this piece of code into your text editor and save it to a file called tiny.c. You can choose any name so long as it ends with .c, this is the extension used by C programs, GCC checks for this extension when compiling a program. With the file saved, you can now compile it into an executable program by typing:

ciaran@pooh:~/book$ gcc tiny.c
ciaran@pooh:~/book$
    
This command should succeed with no output. If you got any error messages check that you typed the program in correctly. Weighing in at eight characters we'll assume you've gotten this much correct and move on. A file called a.out should have appeared in your directory. This is the machine language program created from the above code, if you run it you will see that it really does nothing at all. The name a.out exists for historical reasons, it is short for assembler output.

Although GCC will compile this code, it isn't strictly complete. If we enable GCC's warnings we will be told what is missing. You are not expected to understand the warning messages right now, we show this only to demonstrate GCC's warnings. You enable warnings by adding the -Wall switch to your compilation command.

ciaran@pooh:~/book$ gcc -Wall tiny.c
tiny.c:2: warning: return-type defaults to `int'
tiny.c: In function `main':
tiny.c:3: warning: control reaches end of non-void function
ciaran@pooh:~/book$
    
These warnings appear because our program is not strictly complete. To get rid of these warnings we must add two more lines. So here's the smallest valid C program.

Example 1-3. tiny2.c

int
main()
{
  return 0;
}
     
When we compile this with the -Wall option we will get no warnings. Another option: -o filename can be used to specify the name you want to give to your program (instead of a.out).
ciaran@pooh:~/book$ gcc -Wall -o tiny-program tiny2.c
ciaran@pooh:~/book$ ls
tiny2.c  tiny-program
ciaran@pooh:~/book$ ./tiny-program
ciaran@pooh:~/book$