Compilation Process

advertisement

cs3843 syllabus outline lecture notes programming assignments recitations homework set up

Compilation Process for C

Preprocess - converts preprocessor directives (e.g., #define,

#include) to their corresponding C code

Compile - generates assembly language from the C code

Assemble - generates machine instructions for the assembly language statements

Link - resolves external references to create an executable

Preprocessor

Please see the preprocessor document for more information

We typically don't keep the results from preprocessing. It is simply an intermediate that gcc discards. To keep the preprocessor results:

gcc -E -o filename.list filename.c

This produces a filename.list file. By default, -E directs its output to stdout.

Compiler

The compiler parses a C file or preprocessed C file to create an assembly language file. To generate and keep the assembly language using gcc:

gcc -O1 -S filename.c

This produces a filename.s file. That is an "oh" for Optimize.

We are telling the compiler how much optimization of the code it should do. In many of our examples, we will use gcc -

O0 which does very few optimizations.

The generated assembly language will contain many instructions which have operator names like movl (move long). It will also include external reference names.

Assembler

The assembler uses an assembly file as input. It generates actual machine code and will also include references to

Our example will have four source files: averageDriver.c - driver which invokes readStudents.c and calculateAverage.c readStudents.c - reads an array of student records calculateAverage.c - prints an array of student records student.h - include file which defines Student and StudentData.

Examine the contents of averageDriver.c and student.h.

Execute gcc -E -o avg.list averageDriver.c

Examine the contents of avg.list

Why does it have so much in it?

Examine the contents of calculateAverage.c.

Execute gcc -O1 -S calculateAverage.c

Examine the contents of calculateAverage.s

Execute gcc -O1 -S readStudents.c

Examine the contents of readStudents.s

Execute gcc -O1 -S averageDriver.c

Examine the contents of averageDriver.s

Use gcc to generate the object files (.o) for readStudents.s and calculateAverage.s. gcc -c readStudents.s calculateAverage.s

external names.

gcc can take an assembler file as input and generate the corresponding .o (machine object) file:

gcc -c filename.s

Linker

The linker creates an executable from machine object files. It resolves external references.

Use gcc to directly create the .o for averageDriver.c gcc -c averageDriver.c

Create the studAvg executable linking all the .o files. gcc -o studAvg averageDriver.o readStudents.o

calculateAverage.o

Download