CS 162 Intro to Computer Science II Separate Compila4on - - PowerPoint PPT Presentation

cs 162 intro to computer science ii
SMART_READER_LITE
LIVE PREVIEW

CS 162 Intro to Computer Science II Separate Compila4on - - PowerPoint PPT Presentation

CS 162 Intro to Computer Science II Separate Compila4on 1 Compila4on #include <iostream> int main() { std::cout << Hello world


slide-1
SLIDE 1

CS ¡162 ¡ Intro ¡to ¡Computer ¡Science ¡II ¡

Separate ¡Compila4on ¡

1 ¡

slide-2
SLIDE 2

Compila4on ¡

¡ #include ¡<iostream> ¡ int ¡main() ¡{ ¡ std::cout ¡<< ¡“Hello ¡world” ¡<< ¡std::endl; ¡ return ¡0; ¡ } ¡

2 ¡

slide-3
SLIDE 3

Executable ¡Files ¡

¡ Compile ¡with: ¡ ¡g++ ¡program1.cpp ¡ Executable ¡file ¡is ¡the ¡default, ¡a.out ¡ Or ¡ ¡g++ ¡program1.cpp ¡-­‑o ¡program1 ¡ Executable ¡file ¡is ¡named ¡program1 ¡ ¡

3 ¡

slide-4
SLIDE 4

Separate ¡Compila4on ¡

  • Create ¡separate ¡object ¡files: ¡ ¡

¡g++ ¡program1.cpp ¡ ¡ ¡-­‑o ¡program1.o ¡

  • Logical ¡parts ¡of ¡your ¡program ¡are ¡in ¡different ¡

files ¡ ¡

– Only ¡need ¡to ¡change ¡the ¡file ¡ ¡ ¡ – Only ¡recompiles ¡the ¡changed ¡object ¡files ¡ ¡

  • The ¡linker ¡brings ¡together ¡all ¡the ¡necessary ¡

files ¡ ¡

4 ¡

slide-5
SLIDE 5

Header ¡Files ¡

  • Separate ¡implementa4on ¡from ¡the ¡interface ¡ ¡
  • Example ¡ ¡

– Defini4on ¡for ¡square() ¡goes ¡in ¡func4ons.cpp ¡ – The ¡func4on ¡prototype ¡(defini4on) ¡goes ¡into ¡ func4ons.hpp ¡ ¡ – Must ¡include ¡func4ons. ¡hpp ¡in ¡func4ons.cpp ¡

5 ¡

slide-6
SLIDE 6

Example ¡

func4ons.cpp ¡

#include “functions.hpp” int square(int x) { return x * x; }

func4ons.hpp ¡

int square(int x)

6 ¡

slide-7
SLIDE 7

Extended ¡Example ¡

program1.cpp ¡

#include “functions.hpp” int main() { std::cout << square(12) << std::endl; return 0; }

7 ¡

slide-8
SLIDE 8

Extended ¡Example ¡

Compile ¡with: ¡ ¡

G++ program1.cpp functions.cpp –o program1

Produces ¡an ¡executable ¡file ¡called ¡program1 ¡ ¡

8 ¡

slide-9
SLIDE 9

Extended ¡Example ¡

To ¡create ¡an ¡object ¡file, ¡just ¡the ¡compila4on ¡of ¡

  • ne ¡file ¡use: ¡ ¡

g++ functions.cpp –c –o functions.o

func4ons.o ¡is ¡not ¡executable! ¡ ¡ ¡

This ¡command ¡will ¡pull ¡the ¡parts ¡together ¡to ¡ make ¡an ¡executable ¡file: ¡ ¡

g++ program1.cpp functions.o –o program1

9 ¡

slide-10
SLIDE 10

Include-­‑ ¡issue ¡

  • What ¡if ¡you ¡use ¡#include ¡foo.hpp ¡in ¡10 ¡

implementa4on ¡files? ¡ ¡ ¡ – The ¡code ¡for ¡foo.cpp ¡gets ¡included ¡in ¡your ¡ executable ¡file ¡10 ¡4mes! ¡ ¡ ¡

  • Use ¡guards ¡to ¡prevent ¡this ¡ ¡

– What’s ¡a ¡guard? ¡ ¡

#ifndef FOO_HPP #define FOO_HPP … #endif

10 ¡

slide-11
SLIDE 11

Guard ¡Example ¡

func4ons.hpp ¡

#ifndef FUNCTIONS_HPP #define FUNCTIONS_HPP int square(int x); #endif

11 ¡

slide-12
SLIDE 12

Modulariza4on ¡

  • Separate ¡compila4on ¡organizes ¡your ¡code ¡

– Each ¡logical ¡part ¡of ¡the ¡program ¡should ¡be ¡in ¡a ¡ separate ¡file ¡ ¡ – Other ¡programmers ¡do ¡not ¡need ¡to ¡see ¡the ¡ “innards” ¡of ¡your ¡code ¡ ¡

  • You ¡should ¡see ¡classes ¡as ¡an ¡extension ¡of ¡this ¡

concept ¡

– One ¡of ¡the ¡driving ¡forces ¡for ¡the ¡development ¡of ¡

  • bject-­‑oriented ¡programming! ¡ ¡

12 ¡