! -g - - PowerPoint PPT Presentation

g
SMART_READER_LITE
LIVE PREVIEW

! -g - - PowerPoint PPT Presentation

-o > g++ -o hello hello.C > ./hello CS2141 Software Development


slide-1
SLIDE 1
  • CS2141 – Software Development using C/C++

Compiling a C++ Program 2

  • #include <iostream>

using namespace std; int main( ) { cout << “Hello world!” << endl; }

g++ hello.C

  • a.out

./a.out Compiling a C++ Program 3

  • o

> g++ -o hello hello.C > ./hello Hello world!

!

  • g
  • Wall!

" > g++ -g -Wall -o hello hello.C

Compiling a C++ Program 4

  • #"
  • > g++ -o quack quack.C moo.C

$%

  • c

> g++ -c quack.C > g++ -c moo.C

quack.omoo.o

slide-2
SLIDE 2

Compiling a C++ Program 5

  • %&

> g++ -o quack quack.o moo.o

"

> g++ -c quack.C > g++ -o quack quack.o moo.o

%

& %

Compiling a C++ Program 6

  • 'g++ hello.C
  • (hello.C

&

hello.C

#include <iostream> using namespace std int main( ) { int i, n; cin >> n; for( i = 0; i < cout << “Hell cout << “From “ return 0; } .file "hello.C .local _ZSt8__i .comm _ZSt8__i .text .align 2 .globl main .type main, @f main: .LFB1512: pushl %ebp .LCFI0: movl %esp, %e .LCFI1: subl $24, %es

hello.s

01011101010101011 10101011010101100 10100001001111001 00001011110110101 01011100001010100 00001110101100111 01010101100101010 01010101101000011 00010010101010100 11001011011011010 10101010101001011 10111100010101111 11100000001010100

hello.o a.out

  • )&
# 1 "hello.C" # 1 "<built-in>" # 1 "<command line # 1 "hello.C" # 1 "/usr/include/ # 42 "/usr/include # 43 "/usr/include # 4294967220 "/usr # 44 "/usr/include # 1 "/usr/include/ # 35 "/usr/include

hello.ii

*

Compiling a C++ Program 7

  • cpp hello.C > hello.ii
  • +,

g++ -s hello.C

  • g++ -c hello.C
  • hello.C
#include <iostream> using namespace std int main( ) { int i, n; cin >> n; for( i = 0; i < cout << “Hell cout << “From “ return 0; } .file "hello.C .local _ZSt8__i .comm _ZSt8__i .text .align 2 .globl main .type main, @f main: .LFB1512: pushl %ebp .LCFI0: movl %esp, %e .LCFI1: subl $24, %es

hello.s

01011101010101011 10101011010101100 10100001001111001 00001011110110101 01011100001010100 00001110101100111 01010101100101010 01010101101000011 00010010101010100 11001011011011010 10101010101001011 10111100010101111 11100000001010100

hello.o a.out

  • )&
# 1 "hello.C" # 1 "<built-in>" # 1 "<command line # 1 "hello.C" # 1 "/usr/include/ # 42 "/usr/include # 43 "/usr/include # 4294967220 "/usr # 44 "/usr/include # 1 "/usr/include/ # 35 "/usr/include

hello.ii

*

Compiling a C++ Program 8

  • *$

+,&

  • +,

#include$ #include <file.h>

  • #include “file.h”
slide-3
SLIDE 3

Compiling a C++ Program 9

  • #include$
  • ( &

(.

$

  • #ifndef FILENAME_H

// If FILENAME_H is not defined ... #define FILENAME_H // ... then define FILENAME_H and ... ... // ... whatever else ... #endif // ... up until here.

Compiling a C++ Program 10

  • hello.h"hello.C"main.C

hello.h#

  • #ifndef HELLO_H

#define HELLO_H void hello( ); void goodbye( int ); #endif

Compiling a C++ Program 11

  • hello.Chello.h

#include <iostream> using namespace std; #include “hello.h” void hello( ) { cout << “Hello world!” << endl; } void goodbye( int n ) { for( int i = 0; i < n; ++i ) cout << “Goodbye!” << endl; }

Compiling a C++ Program 12

  • main.C

#include “hello.h” int main( ) { hello( ); goodbye( 28 ); return 0; }

slide-4
SLIDE 4

Compiling a C++ Program 13

  • > g++ -c hello.C

> g++ -c main.C > g++ -o hello hello.o main.o

$

"

/&$

Makefile"

make

> make

Compiling a C++ Program 14

  • &"

&&

target: dependencies (tab) command (tab) another command ... ...

# Compiling a C++ Program 15

  • $&

hello: hello.C hello.h main.C g++ -o hello hello.C main.C

&

> make > ./hello Hello world! Goodbye! ...

$&

make$

Compiling a C++ Program 16

!

&

hello: hello.o main.o g++ -o hello hello.o main.o hello.o: hello.C hello.h g++ -c hello.C main.o: main.C hello.h g++ -c main.C

make$

slide-5
SLIDE 5

Compiling a C++ Program 17

  • 1&clean

$ %

# ... other targets ... clean: rm -f hello main.o hello.o

2"make

&clean"% make

> make clean

Compiling a C++ Program 18

"#

1&

3

$

VARIABLENAME = value

$

${VARIABLENAME}

!$

CXX EXEC CXXFLAGS

Compiling a C++ Program 19

$

CXX = g++ CXXFLAGS = -g EXEC = hello OBJS = hello.o main.o # List of object files needed to # build the executable. ${EXEC}: ${OBJS} ${CXX} ${CXXFLAGS}

  • o ${EXEC} ${OBJS}

hello.o: hello.C hello.h ${CXX} ${CXXFLAGS}

  • c hello.C

main.o: main.C hello.h ${CXX} ${CXXFLAGS}

  • c main.C

clean: rm -f ${EXEC} ${OBJS}

Compiling a C++ Program 20

%#&

*.s1

*.s2"s1s2

$

  • $<

$@ $^

.SUFFIXES: s1 s2 ... sn

slide-6
SLIDE 6

Compiling a C++ Program 21

'

.SUFFIXES: .C .o CXX = g++ CXXFLAGS = -g EXEC = hello OBJS = hello.o main.o ${EXEC}: ${OBJS} ${CXX} ${CXXFLAGS}

  • o ${EXEC} ${OBJS}

.C.o: # Abstract rule ${CXX} ${CXXFLAGS}

  • c $<

# Still need to list the dependencies for object files hello.o: hello.C hello.h main.o: main.C hello.h clean: rm -f ${EXEC} ${OBJS}