If your web browser formats this page incorrectly, try viewing the
page source. In netscape 4, click the view menu, then click page source.
To compile a simple C program with gcc, put the source code of your simple C
program in one file named something like foo.c, and run "gcc -o foo foo.c".
This will create a file named foo, which is your program in executable
binary form. File foo will be created with permissions of 0775.
Most programs of medium complexity are written in several *.c and *.h files.
These are usually compiled in two steps. First compile each *.c file into an
*.o file (which is called an object file) by doing "gcc -c $FILE" for each
*.c file. Then link the *.o files into the executable binary by doing "gcc
-o output *.o". 'output' is the name which the executable binary
file will have.
gcc can compile both c and c++ source code. gcc can usually determine
whether the source code is c or c++. If gcc is unsure if the source code is
c or c++, gcc assumes c. To force gcc to assume c++, use g++ instead of gcc.
g++ and gcc are the same program. The difference between g++ and gcc is that
g++ assumes c++ while gcc assumes c. So usually you use gcc instead of g++,
and let gcc figure out if the source code is c or c++. But if you have some
c++ source code which gcc thinks is c, then either use g++ or fix the source
code so that gcc recognizes the source code is c++.
When gcc links *.o files into an executable binary, gcc automatically
includes libc and libgcc. If you need to include more libraries besides libc
and libgcc, add those libraries after the *.o files. For example, to include
libm and libncurses, use "gcc -o output *.o -lm -lncurses".
-W* options are warnings. If you are not debugging a program, then warnings
are useless.
-g* options are for debugging. If you are not debugging a program, then -g*
options are useless.
-O* options are optimizations and are usually a good idea, but are not
needed.