To compile a simple C program type:
gcc program.c
or to compile a simple C++ program type
g++ program.c
If there are errors they will be displayed and compilation will stop, else the executable will be called 'a.out' in the current directory. Note: GCC and G++ take the same options, if you are using C++ simply substitute g++ wherever gcc is below.
If you want the output to be put in a different file you must specify the file with the -o option. Example:
gcc program.c -o program
which will compile file program.c and put the executable in program.
Keep in mind that GCC by default optimizes for compilation speed, this is good during program testing where things will be compiled many times, but the resulting executables are significantly smaller. For program testing the option '-Wall' print all warning messages about program anomalies.
For the final binary one should at least use the option '-O2' which will turn on most optimizations that do not have a space-speed tradeoff. This will result in a faster and smaller executable but will take longer to compile.
If a header file is located somewhere on the system but GCC can't find it simply include the pathname to the directory it's in with an -I option. Such as gcc -I/usr/include/database
gcc program.c will compile the file program.c and put the output in file 'a.out'
gcc -Wall program.c will compile program.c and print all warning messages, the final output will be put into file 'a.out'
gcc program.c -o program will compile the file program.c and put the output in file 'program'
gcc -O2 program.c -o program will compile the file program.c with optimization. The result will be a faster, smaller executable called 'program'.
The following is a list of some utilities, with brief descriptions, which programmers have found useful:
strip Removes symbols and relocation bits. This reduces the size of the binary but prevents debugging.
time Times a program. For example:
$ time a.out
output of a.out
...
1.93u 0.74s 0:14.33 18.6%
ldd Using ldd with the -s option
in the form "ldd -s programname" is useful for identifying which libraries are
needed
for the program
gcclint GNU offers a lint package called "gcclint".
This is preferable to "lint" when using "gcc" to compile. To use
"gcclint" include "-Wall" on the "gcc" command line. Example:
$ gcc -o myprog -Wall rt.c pwr.c