The main function usually call (invoke) other functions during the program execution.
#include <iostream.h> // Header file containing useful definitions...
int main(int argc, char *argv[])
{
cout << "Hello World\n";
}
|
CC [options] ProgramFileName.C |
a.out |
| Option | Meaning | Example | What does it do |
|---|---|---|---|
| -o FileName | Rename the executable output file | CC -o myProg myProg.C |
Compiler will rename executable output file from a.out to myProg |
| -g | Debug Mode | CC -g myProg.C |
Compiler will insert additional information in output a.out that can be use with a debugger (a tool to help programmers find errors in the program) |
| -c | Create relocatable object code | CC -c myProg.C |
Compiler will NOT produce a directly executable computer program, but
a program that can be combined with other "relocatable" codes
NOTE: use this option when your program consists of multiple text files |
| -Ox | Optimize program to level "x" (x = 1, 2, 3 or 4) | CC -O1 myProg.C CC -O2 myProg.C CC -O3 myProg.C CC -O4 myProg.C |
Compiler will try to produce faster running code in output a.out (compiler will run slower, so use this only when your program is tested correctly |
| -fast | Optimize program | CC -fast myProg.C |
Compiler will produce faster running code in output a.out (compiler will run slower, so use this only when your program is tested correctly |
| -Idirectory | Specify an additional include directory path | CC -I. myProg.C |
Tell the C++ compiler to look for include files in directory "."
in addition to its system include directories
NOTE: this option must appear multiple times if you need to specify multiple directory paths |
| -Ldirectory | Specify an additional library directory path | CC -LmyLib myProg.C |
Tell the C++ compiler to look for library files in directory "myLib"
in addition to its system library directories.
NOTE: this option is most useful when combined with the -l option below. NOTE: this option must appear multiple times if you need to specify multiple library files |
| -lLibFile | Specify an additional library file | CC myProg.C -lmath |
Tell the C++ compiler to "link in" (include) functions from
the "math" function library.
The actual UNIX filename for a C++ library file "math" is
"libmath.a"
NOTE: NEVER use this option along with -c and this option always appears last on a compiler command !!! |
| -DSymbol=Value | Define a symbol and its value | CC -DMAX=10 myProg.C |
Tell the C++ pre-processor to replace all occurences of "MAX" with "10" in the program file "myProg.C" |
| CC -g -c myProg.C | Produce relocatable object code with debug information |
CC -g -c myFile1.C // This command will produce the // relocatable object file myFile1.o // (which can be combined with other // relocatable object files CC -g -c myFile2.C // This command will produce another // relocatable object file myFile2.o // // We can now combine these files ! CC -o myProg myFile1.o myFile2.o |
Once you have tested your program thoroughly and made sure that it works, then recompile ALL the program files again, now using the -fast option and WITHOUT the -g option:
CC -fast -c myFile1.C CC -fast -c myFile2.C CC -o myProg myFile1.o myFile2.o |
#define SYMBOL (Defines a symbol)
#undef SYMBOL (Undefines a symbol)
#include FILENAME (Insert file FILENAME in this spot)
#if CONDITION (Skip to #endif if CONDITION is false)
#ifdef SYMBOL (Skip to #endif if SYMBOL is NOT DEFINED)
#ifndef SYMBOL (Skip to #endif if SYMBOL is DEFINED)
#else (else part - used with #if...)
#elif ("else if" - used with #if...)
#endif (Used to mark the rang of #ifdef and #ifndef)
|
#ifdef SYMBOL
Text between (#ifdef, #endif) is removed
if SYMBOL is not defined
[#else
... else part
]
#endif
#ifndef SYMBOL
Text between (#ifdef, #endif) is removed
if SYMBOL is defined
[#else
... else part
]
#endif
|
#if CONDITION
Text between (#ifdef, #endif) is removed
if SYMBOL is not defined
[#else
... else part
]
#endif
|
===========
Program.C:
===========
#include "params.h"
int main(int argc, char *argv[])
{
...
}
|
===========
Program.C:
===========
#include "params1.h" // params1.h includes params0.h
#include "params2.h" // params2.h includes params0.h
int main(int argc, char *argv[])
{
...
}
|
For example: params1.h may include params3.h
=========== Trick to prevent a parameter file to be process more than once =========== #ifndef _ThisParameterFileName_ #define _ThisParameterFileName_ .... put the parameter definitions here #endif |
The SECOND time that the parameter file is processed, the symbol _ThisParameterFileName_ HAS ALREADY BEEN defined.
As a result, the entire file (till #endif) is skipped...
Look inside "nested-include.i" to see the effect of the "ifdef"