Previous: Complete Program, Line by Line, Up: A Complete Program [Contents][Index]
To run a C program requires converting the source code into an
executable file. This is called compiling the program,
and the command to do that using GNU C is gcc
.
This example program consists of a single source file. If we call that file fib1.c, the complete command to compile it is this:
gcc -g -O -o fib1 fib1.c
Here, -g says to generate debugging information, -O says to optimize at the basic level, and -o fib1 says to put the executable program in the file fib1.
To run the program, use its file name as a shell command. For instance,
./fib1
However, unless you are sure the program is correct, you should expect to need to debug it. So use this command,
gdb fib1
which starts the GDB debugger (see A Sample GDB Session in Debugging with GDB) so you can run and
debug the executable program fib1
.
Richard Stallman’s advice, from personal experience, is to turn to the debugger as soon as you can reproduce the problem. Don’t try to avoid it by using other methods instead—occasionally they are shortcuts, but usually they waste an unbounded amount of time. With the debugger, you will surely find the bug in a reasonable time; overall, you will get your work done faster. The sooner you get serious and start the debugger, the sooner you are likely to find the bug.
See Compilation, for an introduction to compiling more complex programs which consist of more than one source file.
Previous: Complete Program, Line by Line, Up: A Complete Program [Contents][Index]