C Introduction Flashcards
Phases of compiling and running C programs
1) Program is created using editor and stored on disk
2) Preprocessor program processes the code
3) Compiler creates the object code and stores it on disk
4) Linker links object code with libraries, create .out and stores on disk
5) Loader puts program in memory
6) CPU takes each instruction and executes it, storing new data values as the program executes.
Four types of files for compilers
- source code files
- header files
- object files
- binary executable files
Source Code files
Contain function definitions.
Header files
Contain function declarations, various preprocessor statements and allows source files to access externally-defined functions.
Object files
The output of the compiler, and contains function definitions in binary form.
These are also not executable by themselves
Binary Executable files
The output of the linker, and made from object files.
These can be directly executed.
The Preprocessor (cpp or c preprocessor)
Before the C compiler starts compiling a source code file, the file is processed by the preprocessor.
Invoked automatically by compiler before compilation properly begins.
Converts source code files.
Preprocessor commands
Start with a hashtag.
include
Accesses function definitions defined outside the source code.
define
Mainly defines constants.
Other preprocessor examples
if
#else
#elif
#endif
#error
The Compiler
After the preprocessor includes all header files and expands out all the preprocessor statements, the compiler then can compile the program.
Turns the source code file into an object code file, which contains the binary version of the source code, not the executable version.
Invoking the compiler
May be invoked as either
% gcc foo.c
or
% gcc -c foo.c
This tells the computer to run the preprocessor on the file foo.c, and then compile it into an object file foo.o.
-c
In the second example, -c means the we compile the source code into an object file but we do not invoke the linker.
Invoking the compiler (one source code file):
% gcc foo.c -o foo
This tells the compiler to run the preprocessor on the file foo.c, compile, and link it to create an executable called foo.exe.