120
6.s096 Introduction to C and C++ 1

6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

Embed Size (px)

Citation preview

Page 1: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

6s096 Introduction to C and C++

1

Why 2

1

You seek performance

3

1

You seek performance ldquozero-overhead principlerdquo

4

2 You seek to interface

directly with hardware

5

3

Thatrsquos kinda it

6

C a nice way to avoid writing assembly language directly

7

C++ responds to the demands of maintaining large C projects

8

C++11 responds to the demands of

maintaining large C++ projects

9

Maintain power and flexibility of what came before

10

Today CompilationPipeline

11

Source Code

Program 12

mainc prog

int a = 1 ) prog

gcc -o prog mainc 13

mainc

int a = 1 prog

) prog main2c

int b = 2

gcc -o prog mainc main2c 14

$ gcc -o prog mainc $ prog Hello World $

15

DONE $ gcc -o prog mainc $ prog Hello World $

16

ldquoTo debug the sausage one must see how it is maderdquo

mdashSomeone probably

17

Mainjava Mainclass

( int a = 1 java Main

18

Mainjava Mainclass

(

mainpyc

(

a = 1

mainpy

int a = 1 java Main

python mainpy

19

Mainjava Mainclass

int a = 1 (

maino

)

prog

(

mainpyc

(

int a = 1

mainc

a = 1

mainpy

int a = 1 java Main

python mainpy

prog

20

21

22

mainc

int a = 1 ( ) int a = 1 prog

Pre-Process

Compile

Link 23

maino prog

mainc

int a = 1int a = 1

Pre-Process

(

main2o

(

) prog

Compile

Link 24

progmaino

Pre-Process

Compile

Link

25

Pre-Process

Compile

Link

26

include

define

ifdef 27

rimshottxt

ba-dum chh

joketxt

A man walks into a bar Ouch include rimshottxt

cpp -P joketxt 28

output

A man walks into a bar Ouch ba-dum chh

cpp -P joketxt 29

doublepy

define fosho def define kthx return define wutz print

fosho double(x) kthx x 2

wutz double(6)

These are called ldquomacrosrdquo

cpp -P doublepy 30

output

def double(x) return x 2

print double(6)

cpp -P doublepy 31

AWESOME

output

def double(x) return x 2

print double(6)

cpp -P doublepy | python

12

cpp -P doublepy 32

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 33

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 34

output

99 bottles of beer on the wall 98 bottles of beer on the wall 97 bottles of beer on the wall

cpp -P beertxt 35

answertxt

Whatrsquos 7 times 6 ifdef REVEAL 42 endif

cpp -P answertxt 36

output

Whatrsquos 7 times 6

cpp -P answertxt 37

output

Whatrsquos 7 times 6

cpp -P answertxt 38

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 2: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

Why 2

1

You seek performance

3

1

You seek performance ldquozero-overhead principlerdquo

4

2 You seek to interface

directly with hardware

5

3

Thatrsquos kinda it

6

C a nice way to avoid writing assembly language directly

7

C++ responds to the demands of maintaining large C projects

8

C++11 responds to the demands of

maintaining large C++ projects

9

Maintain power and flexibility of what came before

10

Today CompilationPipeline

11

Source Code

Program 12

mainc prog

int a = 1 ) prog

gcc -o prog mainc 13

mainc

int a = 1 prog

) prog main2c

int b = 2

gcc -o prog mainc main2c 14

$ gcc -o prog mainc $ prog Hello World $

15

DONE $ gcc -o prog mainc $ prog Hello World $

16

ldquoTo debug the sausage one must see how it is maderdquo

mdashSomeone probably

17

Mainjava Mainclass

( int a = 1 java Main

18

Mainjava Mainclass

(

mainpyc

(

a = 1

mainpy

int a = 1 java Main

python mainpy

19

Mainjava Mainclass

int a = 1 (

maino

)

prog

(

mainpyc

(

int a = 1

mainc

a = 1

mainpy

int a = 1 java Main

python mainpy

prog

20

21

22

mainc

int a = 1 ( ) int a = 1 prog

Pre-Process

Compile

Link 23

maino prog

mainc

int a = 1int a = 1

Pre-Process

(

main2o

(

) prog

Compile

Link 24

progmaino

Pre-Process

Compile

Link

25

Pre-Process

Compile

Link

26

include

define

ifdef 27

rimshottxt

ba-dum chh

joketxt

A man walks into a bar Ouch include rimshottxt

cpp -P joketxt 28

output

A man walks into a bar Ouch ba-dum chh

cpp -P joketxt 29

doublepy

define fosho def define kthx return define wutz print

fosho double(x) kthx x 2

wutz double(6)

These are called ldquomacrosrdquo

cpp -P doublepy 30

output

def double(x) return x 2

print double(6)

cpp -P doublepy 31

AWESOME

output

def double(x) return x 2

print double(6)

cpp -P doublepy | python

12

cpp -P doublepy 32

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 33

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 34

output

99 bottles of beer on the wall 98 bottles of beer on the wall 97 bottles of beer on the wall

cpp -P beertxt 35

answertxt

Whatrsquos 7 times 6 ifdef REVEAL 42 endif

cpp -P answertxt 36

output

Whatrsquos 7 times 6

cpp -P answertxt 37

output

Whatrsquos 7 times 6

cpp -P answertxt 38

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 3: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

1

You seek performance

3

1

You seek performance ldquozero-overhead principlerdquo

4

2 You seek to interface

directly with hardware

5

3

Thatrsquos kinda it

6

C a nice way to avoid writing assembly language directly

7

C++ responds to the demands of maintaining large C projects

8

C++11 responds to the demands of

maintaining large C++ projects

9

Maintain power and flexibility of what came before

10

Today CompilationPipeline

11

Source Code

Program 12

mainc prog

int a = 1 ) prog

gcc -o prog mainc 13

mainc

int a = 1 prog

) prog main2c

int b = 2

gcc -o prog mainc main2c 14

$ gcc -o prog mainc $ prog Hello World $

15

DONE $ gcc -o prog mainc $ prog Hello World $

16

ldquoTo debug the sausage one must see how it is maderdquo

mdashSomeone probably

17

Mainjava Mainclass

( int a = 1 java Main

18

Mainjava Mainclass

(

mainpyc

(

a = 1

mainpy

int a = 1 java Main

python mainpy

19

Mainjava Mainclass

int a = 1 (

maino

)

prog

(

mainpyc

(

int a = 1

mainc

a = 1

mainpy

int a = 1 java Main

python mainpy

prog

20

21

22

mainc

int a = 1 ( ) int a = 1 prog

Pre-Process

Compile

Link 23

maino prog

mainc

int a = 1int a = 1

Pre-Process

(

main2o

(

) prog

Compile

Link 24

progmaino

Pre-Process

Compile

Link

25

Pre-Process

Compile

Link

26

include

define

ifdef 27

rimshottxt

ba-dum chh

joketxt

A man walks into a bar Ouch include rimshottxt

cpp -P joketxt 28

output

A man walks into a bar Ouch ba-dum chh

cpp -P joketxt 29

doublepy

define fosho def define kthx return define wutz print

fosho double(x) kthx x 2

wutz double(6)

These are called ldquomacrosrdquo

cpp -P doublepy 30

output

def double(x) return x 2

print double(6)

cpp -P doublepy 31

AWESOME

output

def double(x) return x 2

print double(6)

cpp -P doublepy | python

12

cpp -P doublepy 32

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 33

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 34

output

99 bottles of beer on the wall 98 bottles of beer on the wall 97 bottles of beer on the wall

cpp -P beertxt 35

answertxt

Whatrsquos 7 times 6 ifdef REVEAL 42 endif

cpp -P answertxt 36

output

Whatrsquos 7 times 6

cpp -P answertxt 37

output

Whatrsquos 7 times 6

cpp -P answertxt 38

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 4: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

1

You seek performance ldquozero-overhead principlerdquo

4

2 You seek to interface

directly with hardware

5

3

Thatrsquos kinda it

6

C a nice way to avoid writing assembly language directly

7

C++ responds to the demands of maintaining large C projects

8

C++11 responds to the demands of

maintaining large C++ projects

9

Maintain power and flexibility of what came before

10

Today CompilationPipeline

11

Source Code

Program 12

mainc prog

int a = 1 ) prog

gcc -o prog mainc 13

mainc

int a = 1 prog

) prog main2c

int b = 2

gcc -o prog mainc main2c 14

$ gcc -o prog mainc $ prog Hello World $

15

DONE $ gcc -o prog mainc $ prog Hello World $

16

ldquoTo debug the sausage one must see how it is maderdquo

mdashSomeone probably

17

Mainjava Mainclass

( int a = 1 java Main

18

Mainjava Mainclass

(

mainpyc

(

a = 1

mainpy

int a = 1 java Main

python mainpy

19

Mainjava Mainclass

int a = 1 (

maino

)

prog

(

mainpyc

(

int a = 1

mainc

a = 1

mainpy

int a = 1 java Main

python mainpy

prog

20

21

22

mainc

int a = 1 ( ) int a = 1 prog

Pre-Process

Compile

Link 23

maino prog

mainc

int a = 1int a = 1

Pre-Process

(

main2o

(

) prog

Compile

Link 24

progmaino

Pre-Process

Compile

Link

25

Pre-Process

Compile

Link

26

include

define

ifdef 27

rimshottxt

ba-dum chh

joketxt

A man walks into a bar Ouch include rimshottxt

cpp -P joketxt 28

output

A man walks into a bar Ouch ba-dum chh

cpp -P joketxt 29

doublepy

define fosho def define kthx return define wutz print

fosho double(x) kthx x 2

wutz double(6)

These are called ldquomacrosrdquo

cpp -P doublepy 30

output

def double(x) return x 2

print double(6)

cpp -P doublepy 31

AWESOME

output

def double(x) return x 2

print double(6)

cpp -P doublepy | python

12

cpp -P doublepy 32

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 33

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 34

output

99 bottles of beer on the wall 98 bottles of beer on the wall 97 bottles of beer on the wall

cpp -P beertxt 35

answertxt

Whatrsquos 7 times 6 ifdef REVEAL 42 endif

cpp -P answertxt 36

output

Whatrsquos 7 times 6

cpp -P answertxt 37

output

Whatrsquos 7 times 6

cpp -P answertxt 38

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 5: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

2 You seek to interface

directly with hardware

5

3

Thatrsquos kinda it

6

C a nice way to avoid writing assembly language directly

7

C++ responds to the demands of maintaining large C projects

8

C++11 responds to the demands of

maintaining large C++ projects

9

Maintain power and flexibility of what came before

10

Today CompilationPipeline

11

Source Code

Program 12

mainc prog

int a = 1 ) prog

gcc -o prog mainc 13

mainc

int a = 1 prog

) prog main2c

int b = 2

gcc -o prog mainc main2c 14

$ gcc -o prog mainc $ prog Hello World $

15

DONE $ gcc -o prog mainc $ prog Hello World $

16

ldquoTo debug the sausage one must see how it is maderdquo

mdashSomeone probably

17

Mainjava Mainclass

( int a = 1 java Main

18

Mainjava Mainclass

(

mainpyc

(

a = 1

mainpy

int a = 1 java Main

python mainpy

19

Mainjava Mainclass

int a = 1 (

maino

)

prog

(

mainpyc

(

int a = 1

mainc

a = 1

mainpy

int a = 1 java Main

python mainpy

prog

20

21

22

mainc

int a = 1 ( ) int a = 1 prog

Pre-Process

Compile

Link 23

maino prog

mainc

int a = 1int a = 1

Pre-Process

(

main2o

(

) prog

Compile

Link 24

progmaino

Pre-Process

Compile

Link

25

Pre-Process

Compile

Link

26

include

define

ifdef 27

rimshottxt

ba-dum chh

joketxt

A man walks into a bar Ouch include rimshottxt

cpp -P joketxt 28

output

A man walks into a bar Ouch ba-dum chh

cpp -P joketxt 29

doublepy

define fosho def define kthx return define wutz print

fosho double(x) kthx x 2

wutz double(6)

These are called ldquomacrosrdquo

cpp -P doublepy 30

output

def double(x) return x 2

print double(6)

cpp -P doublepy 31

AWESOME

output

def double(x) return x 2

print double(6)

cpp -P doublepy | python

12

cpp -P doublepy 32

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 33

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 34

output

99 bottles of beer on the wall 98 bottles of beer on the wall 97 bottles of beer on the wall

cpp -P beertxt 35

answertxt

Whatrsquos 7 times 6 ifdef REVEAL 42 endif

cpp -P answertxt 36

output

Whatrsquos 7 times 6

cpp -P answertxt 37

output

Whatrsquos 7 times 6

cpp -P answertxt 38

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 6: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

3

Thatrsquos kinda it

6

C a nice way to avoid writing assembly language directly

7

C++ responds to the demands of maintaining large C projects

8

C++11 responds to the demands of

maintaining large C++ projects

9

Maintain power and flexibility of what came before

10

Today CompilationPipeline

11

Source Code

Program 12

mainc prog

int a = 1 ) prog

gcc -o prog mainc 13

mainc

int a = 1 prog

) prog main2c

int b = 2

gcc -o prog mainc main2c 14

$ gcc -o prog mainc $ prog Hello World $

15

DONE $ gcc -o prog mainc $ prog Hello World $

16

ldquoTo debug the sausage one must see how it is maderdquo

mdashSomeone probably

17

Mainjava Mainclass

( int a = 1 java Main

18

Mainjava Mainclass

(

mainpyc

(

a = 1

mainpy

int a = 1 java Main

python mainpy

19

Mainjava Mainclass

int a = 1 (

maino

)

prog

(

mainpyc

(

int a = 1

mainc

a = 1

mainpy

int a = 1 java Main

python mainpy

prog

20

21

22

mainc

int a = 1 ( ) int a = 1 prog

Pre-Process

Compile

Link 23

maino prog

mainc

int a = 1int a = 1

Pre-Process

(

main2o

(

) prog

Compile

Link 24

progmaino

Pre-Process

Compile

Link

25

Pre-Process

Compile

Link

26

include

define

ifdef 27

rimshottxt

ba-dum chh

joketxt

A man walks into a bar Ouch include rimshottxt

cpp -P joketxt 28

output

A man walks into a bar Ouch ba-dum chh

cpp -P joketxt 29

doublepy

define fosho def define kthx return define wutz print

fosho double(x) kthx x 2

wutz double(6)

These are called ldquomacrosrdquo

cpp -P doublepy 30

output

def double(x) return x 2

print double(6)

cpp -P doublepy 31

AWESOME

output

def double(x) return x 2

print double(6)

cpp -P doublepy | python

12

cpp -P doublepy 32

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 33

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 34

output

99 bottles of beer on the wall 98 bottles of beer on the wall 97 bottles of beer on the wall

cpp -P beertxt 35

answertxt

Whatrsquos 7 times 6 ifdef REVEAL 42 endif

cpp -P answertxt 36

output

Whatrsquos 7 times 6

cpp -P answertxt 37

output

Whatrsquos 7 times 6

cpp -P answertxt 38

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 7: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

C a nice way to avoid writing assembly language directly

7

C++ responds to the demands of maintaining large C projects

8

C++11 responds to the demands of

maintaining large C++ projects

9

Maintain power and flexibility of what came before

10

Today CompilationPipeline

11

Source Code

Program 12

mainc prog

int a = 1 ) prog

gcc -o prog mainc 13

mainc

int a = 1 prog

) prog main2c

int b = 2

gcc -o prog mainc main2c 14

$ gcc -o prog mainc $ prog Hello World $

15

DONE $ gcc -o prog mainc $ prog Hello World $

16

ldquoTo debug the sausage one must see how it is maderdquo

mdashSomeone probably

17

Mainjava Mainclass

( int a = 1 java Main

18

Mainjava Mainclass

(

mainpyc

(

a = 1

mainpy

int a = 1 java Main

python mainpy

19

Mainjava Mainclass

int a = 1 (

maino

)

prog

(

mainpyc

(

int a = 1

mainc

a = 1

mainpy

int a = 1 java Main

python mainpy

prog

20

21

22

mainc

int a = 1 ( ) int a = 1 prog

Pre-Process

Compile

Link 23

maino prog

mainc

int a = 1int a = 1

Pre-Process

(

main2o

(

) prog

Compile

Link 24

progmaino

Pre-Process

Compile

Link

25

Pre-Process

Compile

Link

26

include

define

ifdef 27

rimshottxt

ba-dum chh

joketxt

A man walks into a bar Ouch include rimshottxt

cpp -P joketxt 28

output

A man walks into a bar Ouch ba-dum chh

cpp -P joketxt 29

doublepy

define fosho def define kthx return define wutz print

fosho double(x) kthx x 2

wutz double(6)

These are called ldquomacrosrdquo

cpp -P doublepy 30

output

def double(x) return x 2

print double(6)

cpp -P doublepy 31

AWESOME

output

def double(x) return x 2

print double(6)

cpp -P doublepy | python

12

cpp -P doublepy 32

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 33

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 34

output

99 bottles of beer on the wall 98 bottles of beer on the wall 97 bottles of beer on the wall

cpp -P beertxt 35

answertxt

Whatrsquos 7 times 6 ifdef REVEAL 42 endif

cpp -P answertxt 36

output

Whatrsquos 7 times 6

cpp -P answertxt 37

output

Whatrsquos 7 times 6

cpp -P answertxt 38

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 8: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

C++ responds to the demands of maintaining large C projects

8

C++11 responds to the demands of

maintaining large C++ projects

9

Maintain power and flexibility of what came before

10

Today CompilationPipeline

11

Source Code

Program 12

mainc prog

int a = 1 ) prog

gcc -o prog mainc 13

mainc

int a = 1 prog

) prog main2c

int b = 2

gcc -o prog mainc main2c 14

$ gcc -o prog mainc $ prog Hello World $

15

DONE $ gcc -o prog mainc $ prog Hello World $

16

ldquoTo debug the sausage one must see how it is maderdquo

mdashSomeone probably

17

Mainjava Mainclass

( int a = 1 java Main

18

Mainjava Mainclass

(

mainpyc

(

a = 1

mainpy

int a = 1 java Main

python mainpy

19

Mainjava Mainclass

int a = 1 (

maino

)

prog

(

mainpyc

(

int a = 1

mainc

a = 1

mainpy

int a = 1 java Main

python mainpy

prog

20

21

22

mainc

int a = 1 ( ) int a = 1 prog

Pre-Process

Compile

Link 23

maino prog

mainc

int a = 1int a = 1

Pre-Process

(

main2o

(

) prog

Compile

Link 24

progmaino

Pre-Process

Compile

Link

25

Pre-Process

Compile

Link

26

include

define

ifdef 27

rimshottxt

ba-dum chh

joketxt

A man walks into a bar Ouch include rimshottxt

cpp -P joketxt 28

output

A man walks into a bar Ouch ba-dum chh

cpp -P joketxt 29

doublepy

define fosho def define kthx return define wutz print

fosho double(x) kthx x 2

wutz double(6)

These are called ldquomacrosrdquo

cpp -P doublepy 30

output

def double(x) return x 2

print double(6)

cpp -P doublepy 31

AWESOME

output

def double(x) return x 2

print double(6)

cpp -P doublepy | python

12

cpp -P doublepy 32

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 33

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 34

output

99 bottles of beer on the wall 98 bottles of beer on the wall 97 bottles of beer on the wall

cpp -P beertxt 35

answertxt

Whatrsquos 7 times 6 ifdef REVEAL 42 endif

cpp -P answertxt 36

output

Whatrsquos 7 times 6

cpp -P answertxt 37

output

Whatrsquos 7 times 6

cpp -P answertxt 38

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 9: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

C++11 responds to the demands of

maintaining large C++ projects

9

Maintain power and flexibility of what came before

10

Today CompilationPipeline

11

Source Code

Program 12

mainc prog

int a = 1 ) prog

gcc -o prog mainc 13

mainc

int a = 1 prog

) prog main2c

int b = 2

gcc -o prog mainc main2c 14

$ gcc -o prog mainc $ prog Hello World $

15

DONE $ gcc -o prog mainc $ prog Hello World $

16

ldquoTo debug the sausage one must see how it is maderdquo

mdashSomeone probably

17

Mainjava Mainclass

( int a = 1 java Main

18

Mainjava Mainclass

(

mainpyc

(

a = 1

mainpy

int a = 1 java Main

python mainpy

19

Mainjava Mainclass

int a = 1 (

maino

)

prog

(

mainpyc

(

int a = 1

mainc

a = 1

mainpy

int a = 1 java Main

python mainpy

prog

20

21

22

mainc

int a = 1 ( ) int a = 1 prog

Pre-Process

Compile

Link 23

maino prog

mainc

int a = 1int a = 1

Pre-Process

(

main2o

(

) prog

Compile

Link 24

progmaino

Pre-Process

Compile

Link

25

Pre-Process

Compile

Link

26

include

define

ifdef 27

rimshottxt

ba-dum chh

joketxt

A man walks into a bar Ouch include rimshottxt

cpp -P joketxt 28

output

A man walks into a bar Ouch ba-dum chh

cpp -P joketxt 29

doublepy

define fosho def define kthx return define wutz print

fosho double(x) kthx x 2

wutz double(6)

These are called ldquomacrosrdquo

cpp -P doublepy 30

output

def double(x) return x 2

print double(6)

cpp -P doublepy 31

AWESOME

output

def double(x) return x 2

print double(6)

cpp -P doublepy | python

12

cpp -P doublepy 32

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 33

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 34

output

99 bottles of beer on the wall 98 bottles of beer on the wall 97 bottles of beer on the wall

cpp -P beertxt 35

answertxt

Whatrsquos 7 times 6 ifdef REVEAL 42 endif

cpp -P answertxt 36

output

Whatrsquos 7 times 6

cpp -P answertxt 37

output

Whatrsquos 7 times 6

cpp -P answertxt 38

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 10: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

Maintain power and flexibility of what came before

10

Today CompilationPipeline

11

Source Code

Program 12

mainc prog

int a = 1 ) prog

gcc -o prog mainc 13

mainc

int a = 1 prog

) prog main2c

int b = 2

gcc -o prog mainc main2c 14

$ gcc -o prog mainc $ prog Hello World $

15

DONE $ gcc -o prog mainc $ prog Hello World $

16

ldquoTo debug the sausage one must see how it is maderdquo

mdashSomeone probably

17

Mainjava Mainclass

( int a = 1 java Main

18

Mainjava Mainclass

(

mainpyc

(

a = 1

mainpy

int a = 1 java Main

python mainpy

19

Mainjava Mainclass

int a = 1 (

maino

)

prog

(

mainpyc

(

int a = 1

mainc

a = 1

mainpy

int a = 1 java Main

python mainpy

prog

20

21

22

mainc

int a = 1 ( ) int a = 1 prog

Pre-Process

Compile

Link 23

maino prog

mainc

int a = 1int a = 1

Pre-Process

(

main2o

(

) prog

Compile

Link 24

progmaino

Pre-Process

Compile

Link

25

Pre-Process

Compile

Link

26

include

define

ifdef 27

rimshottxt

ba-dum chh

joketxt

A man walks into a bar Ouch include rimshottxt

cpp -P joketxt 28

output

A man walks into a bar Ouch ba-dum chh

cpp -P joketxt 29

doublepy

define fosho def define kthx return define wutz print

fosho double(x) kthx x 2

wutz double(6)

These are called ldquomacrosrdquo

cpp -P doublepy 30

output

def double(x) return x 2

print double(6)

cpp -P doublepy 31

AWESOME

output

def double(x) return x 2

print double(6)

cpp -P doublepy | python

12

cpp -P doublepy 32

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 33

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 34

output

99 bottles of beer on the wall 98 bottles of beer on the wall 97 bottles of beer on the wall

cpp -P beertxt 35

answertxt

Whatrsquos 7 times 6 ifdef REVEAL 42 endif

cpp -P answertxt 36

output

Whatrsquos 7 times 6

cpp -P answertxt 37

output

Whatrsquos 7 times 6

cpp -P answertxt 38

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 11: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

Today CompilationPipeline

11

Source Code

Program 12

mainc prog

int a = 1 ) prog

gcc -o prog mainc 13

mainc

int a = 1 prog

) prog main2c

int b = 2

gcc -o prog mainc main2c 14

$ gcc -o prog mainc $ prog Hello World $

15

DONE $ gcc -o prog mainc $ prog Hello World $

16

ldquoTo debug the sausage one must see how it is maderdquo

mdashSomeone probably

17

Mainjava Mainclass

( int a = 1 java Main

18

Mainjava Mainclass

(

mainpyc

(

a = 1

mainpy

int a = 1 java Main

python mainpy

19

Mainjava Mainclass

int a = 1 (

maino

)

prog

(

mainpyc

(

int a = 1

mainc

a = 1

mainpy

int a = 1 java Main

python mainpy

prog

20

21

22

mainc

int a = 1 ( ) int a = 1 prog

Pre-Process

Compile

Link 23

maino prog

mainc

int a = 1int a = 1

Pre-Process

(

main2o

(

) prog

Compile

Link 24

progmaino

Pre-Process

Compile

Link

25

Pre-Process

Compile

Link

26

include

define

ifdef 27

rimshottxt

ba-dum chh

joketxt

A man walks into a bar Ouch include rimshottxt

cpp -P joketxt 28

output

A man walks into a bar Ouch ba-dum chh

cpp -P joketxt 29

doublepy

define fosho def define kthx return define wutz print

fosho double(x) kthx x 2

wutz double(6)

These are called ldquomacrosrdquo

cpp -P doublepy 30

output

def double(x) return x 2

print double(6)

cpp -P doublepy 31

AWESOME

output

def double(x) return x 2

print double(6)

cpp -P doublepy | python

12

cpp -P doublepy 32

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 33

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 34

output

99 bottles of beer on the wall 98 bottles of beer on the wall 97 bottles of beer on the wall

cpp -P beertxt 35

answertxt

Whatrsquos 7 times 6 ifdef REVEAL 42 endif

cpp -P answertxt 36

output

Whatrsquos 7 times 6

cpp -P answertxt 37

output

Whatrsquos 7 times 6

cpp -P answertxt 38

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 12: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

Source Code

Program 12

mainc prog

int a = 1 ) prog

gcc -o prog mainc 13

mainc

int a = 1 prog

) prog main2c

int b = 2

gcc -o prog mainc main2c 14

$ gcc -o prog mainc $ prog Hello World $

15

DONE $ gcc -o prog mainc $ prog Hello World $

16

ldquoTo debug the sausage one must see how it is maderdquo

mdashSomeone probably

17

Mainjava Mainclass

( int a = 1 java Main

18

Mainjava Mainclass

(

mainpyc

(

a = 1

mainpy

int a = 1 java Main

python mainpy

19

Mainjava Mainclass

int a = 1 (

maino

)

prog

(

mainpyc

(

int a = 1

mainc

a = 1

mainpy

int a = 1 java Main

python mainpy

prog

20

21

22

mainc

int a = 1 ( ) int a = 1 prog

Pre-Process

Compile

Link 23

maino prog

mainc

int a = 1int a = 1

Pre-Process

(

main2o

(

) prog

Compile

Link 24

progmaino

Pre-Process

Compile

Link

25

Pre-Process

Compile

Link

26

include

define

ifdef 27

rimshottxt

ba-dum chh

joketxt

A man walks into a bar Ouch include rimshottxt

cpp -P joketxt 28

output

A man walks into a bar Ouch ba-dum chh

cpp -P joketxt 29

doublepy

define fosho def define kthx return define wutz print

fosho double(x) kthx x 2

wutz double(6)

These are called ldquomacrosrdquo

cpp -P doublepy 30

output

def double(x) return x 2

print double(6)

cpp -P doublepy 31

AWESOME

output

def double(x) return x 2

print double(6)

cpp -P doublepy | python

12

cpp -P doublepy 32

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 33

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 34

output

99 bottles of beer on the wall 98 bottles of beer on the wall 97 bottles of beer on the wall

cpp -P beertxt 35

answertxt

Whatrsquos 7 times 6 ifdef REVEAL 42 endif

cpp -P answertxt 36

output

Whatrsquos 7 times 6

cpp -P answertxt 37

output

Whatrsquos 7 times 6

cpp -P answertxt 38

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 13: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

mainc prog

int a = 1 ) prog

gcc -o prog mainc 13

mainc

int a = 1 prog

) prog main2c

int b = 2

gcc -o prog mainc main2c 14

$ gcc -o prog mainc $ prog Hello World $

15

DONE $ gcc -o prog mainc $ prog Hello World $

16

ldquoTo debug the sausage one must see how it is maderdquo

mdashSomeone probably

17

Mainjava Mainclass

( int a = 1 java Main

18

Mainjava Mainclass

(

mainpyc

(

a = 1

mainpy

int a = 1 java Main

python mainpy

19

Mainjava Mainclass

int a = 1 (

maino

)

prog

(

mainpyc

(

int a = 1

mainc

a = 1

mainpy

int a = 1 java Main

python mainpy

prog

20

21

22

mainc

int a = 1 ( ) int a = 1 prog

Pre-Process

Compile

Link 23

maino prog

mainc

int a = 1int a = 1

Pre-Process

(

main2o

(

) prog

Compile

Link 24

progmaino

Pre-Process

Compile

Link

25

Pre-Process

Compile

Link

26

include

define

ifdef 27

rimshottxt

ba-dum chh

joketxt

A man walks into a bar Ouch include rimshottxt

cpp -P joketxt 28

output

A man walks into a bar Ouch ba-dum chh

cpp -P joketxt 29

doublepy

define fosho def define kthx return define wutz print

fosho double(x) kthx x 2

wutz double(6)

These are called ldquomacrosrdquo

cpp -P doublepy 30

output

def double(x) return x 2

print double(6)

cpp -P doublepy 31

AWESOME

output

def double(x) return x 2

print double(6)

cpp -P doublepy | python

12

cpp -P doublepy 32

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 33

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 34

output

99 bottles of beer on the wall 98 bottles of beer on the wall 97 bottles of beer on the wall

cpp -P beertxt 35

answertxt

Whatrsquos 7 times 6 ifdef REVEAL 42 endif

cpp -P answertxt 36

output

Whatrsquos 7 times 6

cpp -P answertxt 37

output

Whatrsquos 7 times 6

cpp -P answertxt 38

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 14: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

mainc

int a = 1 prog

) prog main2c

int b = 2

gcc -o prog mainc main2c 14

$ gcc -o prog mainc $ prog Hello World $

15

DONE $ gcc -o prog mainc $ prog Hello World $

16

ldquoTo debug the sausage one must see how it is maderdquo

mdashSomeone probably

17

Mainjava Mainclass

( int a = 1 java Main

18

Mainjava Mainclass

(

mainpyc

(

a = 1

mainpy

int a = 1 java Main

python mainpy

19

Mainjava Mainclass

int a = 1 (

maino

)

prog

(

mainpyc

(

int a = 1

mainc

a = 1

mainpy

int a = 1 java Main

python mainpy

prog

20

21

22

mainc

int a = 1 ( ) int a = 1 prog

Pre-Process

Compile

Link 23

maino prog

mainc

int a = 1int a = 1

Pre-Process

(

main2o

(

) prog

Compile

Link 24

progmaino

Pre-Process

Compile

Link

25

Pre-Process

Compile

Link

26

include

define

ifdef 27

rimshottxt

ba-dum chh

joketxt

A man walks into a bar Ouch include rimshottxt

cpp -P joketxt 28

output

A man walks into a bar Ouch ba-dum chh

cpp -P joketxt 29

doublepy

define fosho def define kthx return define wutz print

fosho double(x) kthx x 2

wutz double(6)

These are called ldquomacrosrdquo

cpp -P doublepy 30

output

def double(x) return x 2

print double(6)

cpp -P doublepy 31

AWESOME

output

def double(x) return x 2

print double(6)

cpp -P doublepy | python

12

cpp -P doublepy 32

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 33

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 34

output

99 bottles of beer on the wall 98 bottles of beer on the wall 97 bottles of beer on the wall

cpp -P beertxt 35

answertxt

Whatrsquos 7 times 6 ifdef REVEAL 42 endif

cpp -P answertxt 36

output

Whatrsquos 7 times 6

cpp -P answertxt 37

output

Whatrsquos 7 times 6

cpp -P answertxt 38

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 15: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

$ gcc -o prog mainc $ prog Hello World $

15

DONE $ gcc -o prog mainc $ prog Hello World $

16

ldquoTo debug the sausage one must see how it is maderdquo

mdashSomeone probably

17

Mainjava Mainclass

( int a = 1 java Main

18

Mainjava Mainclass

(

mainpyc

(

a = 1

mainpy

int a = 1 java Main

python mainpy

19

Mainjava Mainclass

int a = 1 (

maino

)

prog

(

mainpyc

(

int a = 1

mainc

a = 1

mainpy

int a = 1 java Main

python mainpy

prog

20

21

22

mainc

int a = 1 ( ) int a = 1 prog

Pre-Process

Compile

Link 23

maino prog

mainc

int a = 1int a = 1

Pre-Process

(

main2o

(

) prog

Compile

Link 24

progmaino

Pre-Process

Compile

Link

25

Pre-Process

Compile

Link

26

include

define

ifdef 27

rimshottxt

ba-dum chh

joketxt

A man walks into a bar Ouch include rimshottxt

cpp -P joketxt 28

output

A man walks into a bar Ouch ba-dum chh

cpp -P joketxt 29

doublepy

define fosho def define kthx return define wutz print

fosho double(x) kthx x 2

wutz double(6)

These are called ldquomacrosrdquo

cpp -P doublepy 30

output

def double(x) return x 2

print double(6)

cpp -P doublepy 31

AWESOME

output

def double(x) return x 2

print double(6)

cpp -P doublepy | python

12

cpp -P doublepy 32

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 33

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 34

output

99 bottles of beer on the wall 98 bottles of beer on the wall 97 bottles of beer on the wall

cpp -P beertxt 35

answertxt

Whatrsquos 7 times 6 ifdef REVEAL 42 endif

cpp -P answertxt 36

output

Whatrsquos 7 times 6

cpp -P answertxt 37

output

Whatrsquos 7 times 6

cpp -P answertxt 38

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 16: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

DONE $ gcc -o prog mainc $ prog Hello World $

16

ldquoTo debug the sausage one must see how it is maderdquo

mdashSomeone probably

17

Mainjava Mainclass

( int a = 1 java Main

18

Mainjava Mainclass

(

mainpyc

(

a = 1

mainpy

int a = 1 java Main

python mainpy

19

Mainjava Mainclass

int a = 1 (

maino

)

prog

(

mainpyc

(

int a = 1

mainc

a = 1

mainpy

int a = 1 java Main

python mainpy

prog

20

21

22

mainc

int a = 1 ( ) int a = 1 prog

Pre-Process

Compile

Link 23

maino prog

mainc

int a = 1int a = 1

Pre-Process

(

main2o

(

) prog

Compile

Link 24

progmaino

Pre-Process

Compile

Link

25

Pre-Process

Compile

Link

26

include

define

ifdef 27

rimshottxt

ba-dum chh

joketxt

A man walks into a bar Ouch include rimshottxt

cpp -P joketxt 28

output

A man walks into a bar Ouch ba-dum chh

cpp -P joketxt 29

doublepy

define fosho def define kthx return define wutz print

fosho double(x) kthx x 2

wutz double(6)

These are called ldquomacrosrdquo

cpp -P doublepy 30

output

def double(x) return x 2

print double(6)

cpp -P doublepy 31

AWESOME

output

def double(x) return x 2

print double(6)

cpp -P doublepy | python

12

cpp -P doublepy 32

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 33

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 34

output

99 bottles of beer on the wall 98 bottles of beer on the wall 97 bottles of beer on the wall

cpp -P beertxt 35

answertxt

Whatrsquos 7 times 6 ifdef REVEAL 42 endif

cpp -P answertxt 36

output

Whatrsquos 7 times 6

cpp -P answertxt 37

output

Whatrsquos 7 times 6

cpp -P answertxt 38

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 17: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

ldquoTo debug the sausage one must see how it is maderdquo

mdashSomeone probably

17

Mainjava Mainclass

( int a = 1 java Main

18

Mainjava Mainclass

(

mainpyc

(

a = 1

mainpy

int a = 1 java Main

python mainpy

19

Mainjava Mainclass

int a = 1 (

maino

)

prog

(

mainpyc

(

int a = 1

mainc

a = 1

mainpy

int a = 1 java Main

python mainpy

prog

20

21

22

mainc

int a = 1 ( ) int a = 1 prog

Pre-Process

Compile

Link 23

maino prog

mainc

int a = 1int a = 1

Pre-Process

(

main2o

(

) prog

Compile

Link 24

progmaino

Pre-Process

Compile

Link

25

Pre-Process

Compile

Link

26

include

define

ifdef 27

rimshottxt

ba-dum chh

joketxt

A man walks into a bar Ouch include rimshottxt

cpp -P joketxt 28

output

A man walks into a bar Ouch ba-dum chh

cpp -P joketxt 29

doublepy

define fosho def define kthx return define wutz print

fosho double(x) kthx x 2

wutz double(6)

These are called ldquomacrosrdquo

cpp -P doublepy 30

output

def double(x) return x 2

print double(6)

cpp -P doublepy 31

AWESOME

output

def double(x) return x 2

print double(6)

cpp -P doublepy | python

12

cpp -P doublepy 32

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 33

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 34

output

99 bottles of beer on the wall 98 bottles of beer on the wall 97 bottles of beer on the wall

cpp -P beertxt 35

answertxt

Whatrsquos 7 times 6 ifdef REVEAL 42 endif

cpp -P answertxt 36

output

Whatrsquos 7 times 6

cpp -P answertxt 37

output

Whatrsquos 7 times 6

cpp -P answertxt 38

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 18: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

Mainjava Mainclass

( int a = 1 java Main

18

Mainjava Mainclass

(

mainpyc

(

a = 1

mainpy

int a = 1 java Main

python mainpy

19

Mainjava Mainclass

int a = 1 (

maino

)

prog

(

mainpyc

(

int a = 1

mainc

a = 1

mainpy

int a = 1 java Main

python mainpy

prog

20

21

22

mainc

int a = 1 ( ) int a = 1 prog

Pre-Process

Compile

Link 23

maino prog

mainc

int a = 1int a = 1

Pre-Process

(

main2o

(

) prog

Compile

Link 24

progmaino

Pre-Process

Compile

Link

25

Pre-Process

Compile

Link

26

include

define

ifdef 27

rimshottxt

ba-dum chh

joketxt

A man walks into a bar Ouch include rimshottxt

cpp -P joketxt 28

output

A man walks into a bar Ouch ba-dum chh

cpp -P joketxt 29

doublepy

define fosho def define kthx return define wutz print

fosho double(x) kthx x 2

wutz double(6)

These are called ldquomacrosrdquo

cpp -P doublepy 30

output

def double(x) return x 2

print double(6)

cpp -P doublepy 31

AWESOME

output

def double(x) return x 2

print double(6)

cpp -P doublepy | python

12

cpp -P doublepy 32

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 33

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 34

output

99 bottles of beer on the wall 98 bottles of beer on the wall 97 bottles of beer on the wall

cpp -P beertxt 35

answertxt

Whatrsquos 7 times 6 ifdef REVEAL 42 endif

cpp -P answertxt 36

output

Whatrsquos 7 times 6

cpp -P answertxt 37

output

Whatrsquos 7 times 6

cpp -P answertxt 38

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 19: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

Mainjava Mainclass

(

mainpyc

(

a = 1

mainpy

int a = 1 java Main

python mainpy

19

Mainjava Mainclass

int a = 1 (

maino

)

prog

(

mainpyc

(

int a = 1

mainc

a = 1

mainpy

int a = 1 java Main

python mainpy

prog

20

21

22

mainc

int a = 1 ( ) int a = 1 prog

Pre-Process

Compile

Link 23

maino prog

mainc

int a = 1int a = 1

Pre-Process

(

main2o

(

) prog

Compile

Link 24

progmaino

Pre-Process

Compile

Link

25

Pre-Process

Compile

Link

26

include

define

ifdef 27

rimshottxt

ba-dum chh

joketxt

A man walks into a bar Ouch include rimshottxt

cpp -P joketxt 28

output

A man walks into a bar Ouch ba-dum chh

cpp -P joketxt 29

doublepy

define fosho def define kthx return define wutz print

fosho double(x) kthx x 2

wutz double(6)

These are called ldquomacrosrdquo

cpp -P doublepy 30

output

def double(x) return x 2

print double(6)

cpp -P doublepy 31

AWESOME

output

def double(x) return x 2

print double(6)

cpp -P doublepy | python

12

cpp -P doublepy 32

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 33

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 34

output

99 bottles of beer on the wall 98 bottles of beer on the wall 97 bottles of beer on the wall

cpp -P beertxt 35

answertxt

Whatrsquos 7 times 6 ifdef REVEAL 42 endif

cpp -P answertxt 36

output

Whatrsquos 7 times 6

cpp -P answertxt 37

output

Whatrsquos 7 times 6

cpp -P answertxt 38

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 20: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

Mainjava Mainclass

int a = 1 (

maino

)

prog

(

mainpyc

(

int a = 1

mainc

a = 1

mainpy

int a = 1 java Main

python mainpy

prog

20

21

22

mainc

int a = 1 ( ) int a = 1 prog

Pre-Process

Compile

Link 23

maino prog

mainc

int a = 1int a = 1

Pre-Process

(

main2o

(

) prog

Compile

Link 24

progmaino

Pre-Process

Compile

Link

25

Pre-Process

Compile

Link

26

include

define

ifdef 27

rimshottxt

ba-dum chh

joketxt

A man walks into a bar Ouch include rimshottxt

cpp -P joketxt 28

output

A man walks into a bar Ouch ba-dum chh

cpp -P joketxt 29

doublepy

define fosho def define kthx return define wutz print

fosho double(x) kthx x 2

wutz double(6)

These are called ldquomacrosrdquo

cpp -P doublepy 30

output

def double(x) return x 2

print double(6)

cpp -P doublepy 31

AWESOME

output

def double(x) return x 2

print double(6)

cpp -P doublepy | python

12

cpp -P doublepy 32

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 33

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 34

output

99 bottles of beer on the wall 98 bottles of beer on the wall 97 bottles of beer on the wall

cpp -P beertxt 35

answertxt

Whatrsquos 7 times 6 ifdef REVEAL 42 endif

cpp -P answertxt 36

output

Whatrsquos 7 times 6

cpp -P answertxt 37

output

Whatrsquos 7 times 6

cpp -P answertxt 38

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 21: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

21

22

mainc

int a = 1 ( ) int a = 1 prog

Pre-Process

Compile

Link 23

maino prog

mainc

int a = 1int a = 1

Pre-Process

(

main2o

(

) prog

Compile

Link 24

progmaino

Pre-Process

Compile

Link

25

Pre-Process

Compile

Link

26

include

define

ifdef 27

rimshottxt

ba-dum chh

joketxt

A man walks into a bar Ouch include rimshottxt

cpp -P joketxt 28

output

A man walks into a bar Ouch ba-dum chh

cpp -P joketxt 29

doublepy

define fosho def define kthx return define wutz print

fosho double(x) kthx x 2

wutz double(6)

These are called ldquomacrosrdquo

cpp -P doublepy 30

output

def double(x) return x 2

print double(6)

cpp -P doublepy 31

AWESOME

output

def double(x) return x 2

print double(6)

cpp -P doublepy | python

12

cpp -P doublepy 32

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 33

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 34

output

99 bottles of beer on the wall 98 bottles of beer on the wall 97 bottles of beer on the wall

cpp -P beertxt 35

answertxt

Whatrsquos 7 times 6 ifdef REVEAL 42 endif

cpp -P answertxt 36

output

Whatrsquos 7 times 6

cpp -P answertxt 37

output

Whatrsquos 7 times 6

cpp -P answertxt 38

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 22: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

22

mainc

int a = 1 ( ) int a = 1 prog

Pre-Process

Compile

Link 23

maino prog

mainc

int a = 1int a = 1

Pre-Process

(

main2o

(

) prog

Compile

Link 24

progmaino

Pre-Process

Compile

Link

25

Pre-Process

Compile

Link

26

include

define

ifdef 27

rimshottxt

ba-dum chh

joketxt

A man walks into a bar Ouch include rimshottxt

cpp -P joketxt 28

output

A man walks into a bar Ouch ba-dum chh

cpp -P joketxt 29

doublepy

define fosho def define kthx return define wutz print

fosho double(x) kthx x 2

wutz double(6)

These are called ldquomacrosrdquo

cpp -P doublepy 30

output

def double(x) return x 2

print double(6)

cpp -P doublepy 31

AWESOME

output

def double(x) return x 2

print double(6)

cpp -P doublepy | python

12

cpp -P doublepy 32

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 33

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 34

output

99 bottles of beer on the wall 98 bottles of beer on the wall 97 bottles of beer on the wall

cpp -P beertxt 35

answertxt

Whatrsquos 7 times 6 ifdef REVEAL 42 endif

cpp -P answertxt 36

output

Whatrsquos 7 times 6

cpp -P answertxt 37

output

Whatrsquos 7 times 6

cpp -P answertxt 38

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 23: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

mainc

int a = 1 ( ) int a = 1 prog

Pre-Process

Compile

Link 23

maino prog

mainc

int a = 1int a = 1

Pre-Process

(

main2o

(

) prog

Compile

Link 24

progmaino

Pre-Process

Compile

Link

25

Pre-Process

Compile

Link

26

include

define

ifdef 27

rimshottxt

ba-dum chh

joketxt

A man walks into a bar Ouch include rimshottxt

cpp -P joketxt 28

output

A man walks into a bar Ouch ba-dum chh

cpp -P joketxt 29

doublepy

define fosho def define kthx return define wutz print

fosho double(x) kthx x 2

wutz double(6)

These are called ldquomacrosrdquo

cpp -P doublepy 30

output

def double(x) return x 2

print double(6)

cpp -P doublepy 31

AWESOME

output

def double(x) return x 2

print double(6)

cpp -P doublepy | python

12

cpp -P doublepy 32

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 33

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 34

output

99 bottles of beer on the wall 98 bottles of beer on the wall 97 bottles of beer on the wall

cpp -P beertxt 35

answertxt

Whatrsquos 7 times 6 ifdef REVEAL 42 endif

cpp -P answertxt 36

output

Whatrsquos 7 times 6

cpp -P answertxt 37

output

Whatrsquos 7 times 6

cpp -P answertxt 38

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 24: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

mainc

int a = 1int a = 1

Pre-Process

(

main2o

(

) prog

Compile

Link 24

progmaino

Pre-Process

Compile

Link

25

Pre-Process

Compile

Link

26

include

define

ifdef 27

rimshottxt

ba-dum chh

joketxt

A man walks into a bar Ouch include rimshottxt

cpp -P joketxt 28

output

A man walks into a bar Ouch ba-dum chh

cpp -P joketxt 29

doublepy

define fosho def define kthx return define wutz print

fosho double(x) kthx x 2

wutz double(6)

These are called ldquomacrosrdquo

cpp -P doublepy 30

output

def double(x) return x 2

print double(6)

cpp -P doublepy 31

AWESOME

output

def double(x) return x 2

print double(6)

cpp -P doublepy | python

12

cpp -P doublepy 32

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 33

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 34

output

99 bottles of beer on the wall 98 bottles of beer on the wall 97 bottles of beer on the wall

cpp -P beertxt 35

answertxt

Whatrsquos 7 times 6 ifdef REVEAL 42 endif

cpp -P answertxt 36

output

Whatrsquos 7 times 6

cpp -P answertxt 37

output

Whatrsquos 7 times 6

cpp -P answertxt 38

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 25: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

Pre-Process

Compile

Link

25

Pre-Process

Compile

Link

26

include

define

ifdef 27

rimshottxt

ba-dum chh

joketxt

A man walks into a bar Ouch include rimshottxt

cpp -P joketxt 28

output

A man walks into a bar Ouch ba-dum chh

cpp -P joketxt 29

doublepy

define fosho def define kthx return define wutz print

fosho double(x) kthx x 2

wutz double(6)

These are called ldquomacrosrdquo

cpp -P doublepy 30

output

def double(x) return x 2

print double(6)

cpp -P doublepy 31

AWESOME

output

def double(x) return x 2

print double(6)

cpp -P doublepy | python

12

cpp -P doublepy 32

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 33

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 34

output

99 bottles of beer on the wall 98 bottles of beer on the wall 97 bottles of beer on the wall

cpp -P beertxt 35

answertxt

Whatrsquos 7 times 6 ifdef REVEAL 42 endif

cpp -P answertxt 36

output

Whatrsquos 7 times 6

cpp -P answertxt 37

output

Whatrsquos 7 times 6

cpp -P answertxt 38

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 26: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

Pre-Process

Compile

Link

26

include

define

ifdef 27

rimshottxt

ba-dum chh

joketxt

A man walks into a bar Ouch include rimshottxt

cpp -P joketxt 28

output

A man walks into a bar Ouch ba-dum chh

cpp -P joketxt 29

doublepy

define fosho def define kthx return define wutz print

fosho double(x) kthx x 2

wutz double(6)

These are called ldquomacrosrdquo

cpp -P doublepy 30

output

def double(x) return x 2

print double(6)

cpp -P doublepy 31

AWESOME

output

def double(x) return x 2

print double(6)

cpp -P doublepy | python

12

cpp -P doublepy 32

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 33

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 34

output

99 bottles of beer on the wall 98 bottles of beer on the wall 97 bottles of beer on the wall

cpp -P beertxt 35

answertxt

Whatrsquos 7 times 6 ifdef REVEAL 42 endif

cpp -P answertxt 36

output

Whatrsquos 7 times 6

cpp -P answertxt 37

output

Whatrsquos 7 times 6

cpp -P answertxt 38

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 27: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

include

define

ifdef 27

rimshottxt

ba-dum chh

joketxt

A man walks into a bar Ouch include rimshottxt

cpp -P joketxt 28

output

A man walks into a bar Ouch ba-dum chh

cpp -P joketxt 29

doublepy

define fosho def define kthx return define wutz print

fosho double(x) kthx x 2

wutz double(6)

These are called ldquomacrosrdquo

cpp -P doublepy 30

output

def double(x) return x 2

print double(6)

cpp -P doublepy 31

AWESOME

output

def double(x) return x 2

print double(6)

cpp -P doublepy | python

12

cpp -P doublepy 32

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 33

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 34

output

99 bottles of beer on the wall 98 bottles of beer on the wall 97 bottles of beer on the wall

cpp -P beertxt 35

answertxt

Whatrsquos 7 times 6 ifdef REVEAL 42 endif

cpp -P answertxt 36

output

Whatrsquos 7 times 6

cpp -P answertxt 37

output

Whatrsquos 7 times 6

cpp -P answertxt 38

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 28: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

rimshottxt

ba-dum chh

joketxt

A man walks into a bar Ouch include rimshottxt

cpp -P joketxt 28

output

A man walks into a bar Ouch ba-dum chh

cpp -P joketxt 29

doublepy

define fosho def define kthx return define wutz print

fosho double(x) kthx x 2

wutz double(6)

These are called ldquomacrosrdquo

cpp -P doublepy 30

output

def double(x) return x 2

print double(6)

cpp -P doublepy 31

AWESOME

output

def double(x) return x 2

print double(6)

cpp -P doublepy | python

12

cpp -P doublepy 32

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 33

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 34

output

99 bottles of beer on the wall 98 bottles of beer on the wall 97 bottles of beer on the wall

cpp -P beertxt 35

answertxt

Whatrsquos 7 times 6 ifdef REVEAL 42 endif

cpp -P answertxt 36

output

Whatrsquos 7 times 6

cpp -P answertxt 37

output

Whatrsquos 7 times 6

cpp -P answertxt 38

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 29: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

output

A man walks into a bar Ouch ba-dum chh

cpp -P joketxt 29

doublepy

define fosho def define kthx return define wutz print

fosho double(x) kthx x 2

wutz double(6)

These are called ldquomacrosrdquo

cpp -P doublepy 30

output

def double(x) return x 2

print double(6)

cpp -P doublepy 31

AWESOME

output

def double(x) return x 2

print double(6)

cpp -P doublepy | python

12

cpp -P doublepy 32

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 33

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 34

output

99 bottles of beer on the wall 98 bottles of beer on the wall 97 bottles of beer on the wall

cpp -P beertxt 35

answertxt

Whatrsquos 7 times 6 ifdef REVEAL 42 endif

cpp -P answertxt 36

output

Whatrsquos 7 times 6

cpp -P answertxt 37

output

Whatrsquos 7 times 6

cpp -P answertxt 38

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 30: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

doublepy

define fosho def define kthx return define wutz print

fosho double(x) kthx x 2

wutz double(6)

These are called ldquomacrosrdquo

cpp -P doublepy 30

output

def double(x) return x 2

print double(6)

cpp -P doublepy 31

AWESOME

output

def double(x) return x 2

print double(6)

cpp -P doublepy | python

12

cpp -P doublepy 32

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 33

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 34

output

99 bottles of beer on the wall 98 bottles of beer on the wall 97 bottles of beer on the wall

cpp -P beertxt 35

answertxt

Whatrsquos 7 times 6 ifdef REVEAL 42 endif

cpp -P answertxt 36

output

Whatrsquos 7 times 6

cpp -P answertxt 37

output

Whatrsquos 7 times 6

cpp -P answertxt 38

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 31: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

output

def double(x) return x 2

print double(6)

cpp -P doublepy 31

AWESOME

output

def double(x) return x 2

print double(6)

cpp -P doublepy | python

12

cpp -P doublepy 32

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 33

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 34

output

99 bottles of beer on the wall 98 bottles of beer on the wall 97 bottles of beer on the wall

cpp -P beertxt 35

answertxt

Whatrsquos 7 times 6 ifdef REVEAL 42 endif

cpp -P answertxt 36

output

Whatrsquos 7 times 6

cpp -P answertxt 37

output

Whatrsquos 7 times 6

cpp -P answertxt 38

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 32: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

AWESOME

output

def double(x) return x 2

print double(6)

cpp -P doublepy | python

12

cpp -P doublepy 32

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 33

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 34

output

99 bottles of beer on the wall 98 bottles of beer on the wall 97 bottles of beer on the wall

cpp -P beertxt 35

answertxt

Whatrsquos 7 times 6 ifdef REVEAL 42 endif

cpp -P answertxt 36

output

Whatrsquos 7 times 6

cpp -P answertxt 37

output

Whatrsquos 7 times 6

cpp -P answertxt 38

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 33: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 33

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 34

output

99 bottles of beer on the wall 98 bottles of beer on the wall 97 bottles of beer on the wall

cpp -P beertxt 35

answertxt

Whatrsquos 7 times 6 ifdef REVEAL 42 endif

cpp -P answertxt 36

output

Whatrsquos 7 times 6

cpp -P answertxt 37

output

Whatrsquos 7 times 6

cpp -P answertxt 38

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 34: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

beertxt

define beer(x) x bottles of beer on the wall

beer(99) beer(98) beer(97)

cpp -P beertxt 34

output

99 bottles of beer on the wall 98 bottles of beer on the wall 97 bottles of beer on the wall

cpp -P beertxt 35

answertxt

Whatrsquos 7 times 6 ifdef REVEAL 42 endif

cpp -P answertxt 36

output

Whatrsquos 7 times 6

cpp -P answertxt 37

output

Whatrsquos 7 times 6

cpp -P answertxt 38

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 35: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

output

99 bottles of beer on the wall 98 bottles of beer on the wall 97 bottles of beer on the wall

cpp -P beertxt 35

answertxt

Whatrsquos 7 times 6 ifdef REVEAL 42 endif

cpp -P answertxt 36

output

Whatrsquos 7 times 6

cpp -P answertxt 37

output

Whatrsquos 7 times 6

cpp -P answertxt 38

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 36: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

answertxt

Whatrsquos 7 times 6 ifdef REVEAL 42 endif

cpp -P answertxt 36

output

Whatrsquos 7 times 6

cpp -P answertxt 37

output

Whatrsquos 7 times 6

cpp -P answertxt 38

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 37: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

output

Whatrsquos 7 times 6

cpp -P answertxt 37

output

Whatrsquos 7 times 6

cpp -P answertxt 38

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 38: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

output

Whatrsquos 7 times 6

cpp -P answertxt 38

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 39: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

define REVEAL

cpp -P answertxt 39

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 40: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

define REVEAL

or

cpp -P -D REVEAL answertxt

cpp -P answertxt 40

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 41: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

output

Whatrsquos 7 times 6 42

cpp -P -D REVEAL answertxt 41

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 42: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

answertxt

Whatrsquos 7 times 6 ifndef REVEAL 42 endif

cpp -P answertxt 42

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 43: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

(Fancy) String Substitution

43

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 44: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

How is this used in C

44

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 45: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

helloc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 45

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 46: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 46

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 47: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

helloc angle brackets -gt use the system search path

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -E helloc 47

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 48: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

output

int printf(const char ) __attribute__((__format__ (__printf__ 1 2)))

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 48

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 49: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

output

int printf(const char )

int main() printf(Hello Worldn)

pretending printf is all thatrsquos defined in stdioh

gcc -E helloc 49

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 50: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

include is not import pickle

import javaio

50

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 51: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

fibc

define MAX_FIB 20 int fib[MAX_FIB]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt MAX_FIB i++)

fib[i] = fib[i-1] + fib[i-2] return 0

gcc -E fibc 51

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 52: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

output

int fib[20]

int main() fib[0] = 0 fib[1] = 1 for(int i = 2 i lt 20 i++)

fib[i] = fib[i-1] + fib[i-2]

gcc -E fibc 52

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 53: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

debugc

include ltstdiohgt

int main() ifdef DEBUG

printf(Hello Worldn) endif

return 0

gcc -DDEBUG debugc -o debug 53

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 54: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

debugc

include ltstdiohgt

int main() printf(Hello Worldn) return 0

gcc -DDEBUG debugc -o debug 54

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 55: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

debugc

include ltstdiohgt

int main() return 0

gcc debugc -o debug 55

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 56: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

Pre-Process

Compile

Link

56

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 57: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

int a = 1 (

maino

)

prog

int a = 1

mainc

prog

57

Compile

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 58: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

Compile

Type-checking Linear processing

58

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 59: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

Type-checking int reptile()

return frog

59

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 60: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

Type-checking int reptile()

return frog

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

60

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 61: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

61

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 62: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

Type-checking def vegetable(day)

if day = Tuesday return tomato

else return 1000

Python says no problem 62

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 63: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

int reptile() return frog

63

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 64: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

int () return char

64

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 65: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

int vegetable(char day) if (strcmp(day Tuesday) = 0)

return tomato else

return 1000

65

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 66: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

int (char) if (int)

return char else

return int

66

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 67: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

int (char) if (int)

return char else

return int

67

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 68: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

int (char) if (int) return char else return int

68

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 69: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

Everything has a single fixed type

69

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 70: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

def foo(a b) return a + b

foo(2 3) foo(2 3)

70

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 71: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

Variable Declarations int foo float foo double foo char foo

int foo[42] int foo struct Bar foo

71

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 72: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

Function Declarations double fmin(double double)

return type argument types

72

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 73: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

Function Declarations void exit(int)

returns nothing

int rand(void)

takes no arguments 73

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 74: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

int foo(int a int b) return a + b

74

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 75: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

75

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 76: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

reptilec In function lsquoreptilersquo reptilec25 warning return makes integer from pointer without a cast

int a = 4 float b = (float)a

76

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 77: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

all theoretical casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

77

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 78: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

78

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 79: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

79

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 80: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

allowed casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 80

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 81: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

allowed casts

int float double char int[] int void (f )(int) struct X

int float double char int[] int void (f )(int) struct X

81

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 82: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) void (f )(int) struct X struct X

82

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 83: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

implicit casts

int float double char int[] int void (f )(int)

int float double char int[] int void (f )(int)

struct X struct X 83

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 84: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

implicit casts

int int float float double double char char int[] int

int[] int

void (f )(int) void (f )(int) struct X struct X

84

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 85: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

implicit casts

int int float float double double char char int[] int[] int int void (f )(int) struct X

void (f )(int) struct X

85

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 86: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

Compile

Type-checking Linear processing

86

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 87: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

Linear processing ( just a small note)87

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 88: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

You can only use whatrsquos declared above

88

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 89: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

int main() printf(dn answer()) return 0

int answer() return 1337

89

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 90: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

int main() printf(dn answer()) return 0

int answer() return 1337

90

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 91: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

int answer() return 1337

int main() printf(dn answer()) return 0

91

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 92: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

92

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 93: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

HMM 93

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 94: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

int answer() declaration

int main() printf(dn answer()) return 0

int answer() definition return 1337

94

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 95: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

int answer()

int main() printf(dn answer()) return 0

declaration

int answer() return 1337

definition

95

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 96: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

int answer() declaration

include answerh

int main() printf(dn answer()) return 0

int answer() return 1337

definition

96

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 97: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

Pre-Process

Compile

Link

97

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 98: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

maino

int main()

int answer()

98

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 99: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

maino

int main()

99

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 100: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

100

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 101: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

101

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 102: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

102

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 103: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

103

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 104: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo Undefined symbols for architecture x86_64

_answer referenced from _main in ccuzmRrmo

ld symbol(s) not found for architecture x86_64collect2 ld returned 1 exit status

Linker ldquoI looked in all the object files but I couldnrsquot find answerrdquo

104

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 105: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

maino

int main()

105

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 106: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

maino answero

int main()

int answer()

106

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 107: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

maino answero

int main()

int answer()

int main() printf(dn answer()) return 0

int answer() return 1337

107

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 108: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

maino answero

int main()

int answer()

108

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 109: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

prog

gcc -o prog mainc answerc

int main()

int answer()

109

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 110: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

110

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 111: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

answerc In function lsquomainrsquo answerc4 warning implicit declaration offunction lsquoanswerrsquo

Compiler ldquoI donrsquot know what answer is Irsquoll assume it returns an intrdquo

111

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 112: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

answerh

int answer()

include answerh

int main()

mainc

printf(dn answer()) return 0

112

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 113: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

answerh

int answer()

answerc

include answerh

int answer() return 1337

113

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 114: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

Summary 114

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 115: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

answerh

int answer()

mainc

include answerh

int main() printf(dn answer()) return 0

115

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 116: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

Preprocess gcc -E mainc

int answer()

int main() printf(dn answer()) return 0

116

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 117: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

Compile gcc -c mainc mainc

maino

(

answero

(

117

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 118: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

Link gcc -o prog maino maino

prog

(

118

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 119: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

Pre-Process

Compile

Link

119

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms

Page 120: 6.S096 Lecture 1: Compilation Pipeline - MIT OpenCourseWare · PDF fileint a = 1; prog main2.c @*)!% ... Main.java Main.class. int a = 1; %!(*@ ... 6.S096 Lecture 1: Compilation Pipeline

MIT OpenCourseWarehttpocwmitedu

6S096 Introduction to C and C++IAP 2013

For information about citing these materials or our Terms of Use visit httpocwmiteduterms