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.
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:
[email protected]:~/book$ gcc tiny.c [email protected]:~/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.
[email protected]:~/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 [email protected]:~/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.
[email protected]:~/book$ gcc -Wall -o tiny-program tiny2.c [email protected]:~/book$ ls tiny2.c tiny-program [email protected]:~/book$ ./tiny-program [email protected]:~/book$