Upload
verity-morton
View
227
Download
2
Embed Size (px)
Citation preview
CS 261 – Data Structures
C – Compilation Process
Compilation ProcessRemove CommentsReplace Pre-processor directives (#).
For example, #include gets replaced with the include file
Conditional compilation
Pre-Processor
source .h, .c
Compiler
source .h, .c
Assembler
assembly .s
Linker
machine .o
executable.exe
#include<stdio.h>
int main(void) {
printf("Hello world!\n");
}
Compilation ProcessTake resulting source code and compiles to assembly code (.s)
Machine level code that manipulates memory and processor
Pre-Processor
source .h, .c
Compiler
source .h, .c
Assembler
assembly .s
Linker
machine .o
executable.exe
.section __TEXT,__text,regular,pure_instructions.globl _main.align 4, 0x90
_main:Leh_func_begin1:
pushq %rbpLtmp0:
movq %rsp, %rbpLtmp1:
subq $16, %rspLtmp2:
movl %edi, %eaxmovl %eax, -4(%rbp)movq %rsi, -16(%rbp)leaq L_.str(%rip), %raxmovq %rax, %rdicallq _putsaddq $16, %rsppopq %rbpret
Leh_func_end1:
.section __TEXT,__cstring,cstring_literalsL_.str:
.asciz "Hello World”
gcc -g -S -Wall -std=c99 -c main.c
Compilation ProcessAssembler generates object code (.o) from the assembly code.
Object code is in binary and can’t be viewed with a text reader
Pre-Processor
source .h, .c
Compiler
source .h, .c
Assembler
assembly .s
Linker
machine .o
executable.exe
gcc –c main.c gcc – c dynArr.c
Pre-process, compile, assemble
Compilation Process The Linker pulls together your object code with libraries that you’re using in your program.
In this case, we use ‘printf’, so it will pull in the c standard libraryThe result is the executable
Pre-Processor
source .h, .c
Compiler
source .h, .c
Assembler
assembly .s
Linker
machine .o
executable.exe
#include<stdio.h>
int main(void) {
printf("Hello world!\n");
}
other libs and.o files
gcc –o prog main.o dynArr.o
Separation of Interface and Implementation
Header files (*.h) have declarations and function prototypes (ie. interfaces)
Implementation files (*.c) have implementation source
A prototype is a function signature but no body (promise that the code will be linked in later)
int max(int a, int b);/* Function prototype */
Allows us to ‘hide’ implementation in pre-compiled .o files
Function prototypes must be terminated with a semicolon.
Source Files - arrayBagStack.h
Source Files - arrayBagStack.c
.c and .h files
client’s program (.c file)
.h file 1 .h file 2
.o file with implementation
of header 1
.o file with implementation
of header 2
your data structure headers (.h)
your data structure code (.o)
not visible to client developer
visible to client developer
linker pulls in the promised code
Tips: Ensuring Declarations Seen only Once
/* Header file for foo.h */
#ifndef BIG_COMPLEX_NAME
#define BIG_COMPLEX_NAME
…
/* Rest of foo.h file. */
#endif
If foo.h is included more than once (e.g., through other included files), it only gets inserted into the source (.c) file once
Conditionallycompiled code!!!
GCC And Make
Also, see the class website for much more on gcc, make and c-programming in general
Managing Projects with make, 2nd EditionThe Power of GNU make for Building AnythingBy Andy Oram, Steve TalbottPublisher: O'Reilly MediaReleased: October 1991Pages: 168