gcc -o c-exe test.c
To compile a C++ program which is in a file named, say test.cc, use the command:
g++ -o c-exe test.cc
Either produces an executable file named c-exe which can then be run by simply typing the name of the executable. If you don't use the option -o, then the executable file is named a.out by default.
A debugger is a powerful tool. It ranks as one of the basic tools that's worth mastering for a programmer. To debug a C/C++ program you can use the source-level debugger gdb. To enable debugging you will need to compile with the right options.
gcc -g -o prog test.c
or
g++ -gstabs+ -o prog test.cc
Then you can invoke the debugger as follows:
gdb prog
The debugger gdb allows you to set breakpoints, examine and change
variables, step through your program, etc. In the debugger there is a help
command which describes the various commands available. A complete
manual is available on-line at the following web address:
http://cs.boisestate.edu/
amit/teaching/handouts/gdb.html
The gdb debugger has a man page as well as an info topic under Linux.
WE also highly recommend the Data Display Debugger ddd for a graphical debugger that allows you to visualize your data structures while the program is running! For more on ddd, visit its web site http://www.gnu.org/software/ddd/.
The C and C++ compilers have many other options; see man gcc or man g++ for more information. One useful option is to link with the math library. You need this if your program is referring to a common mathematical function, e.g. sin(), sqrt(). To link with the math library use the following command:
gcc -o prog test.c -lm
or
g++ -o prog test.cc -lm
The option -lm tells the linker/loader (invoked by the compiler) to look for a library with the name libm.so (so is short for shared object or a shared library). The linker/loader searches a set of standard directories to find the math libraries and then links in your executable with the library so that your executable program can find the mathematical functions when it is running. See the man page for ld and ld.so for more information (ld is short for loader).