cs 162 intro to computer science ii
play

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


  1. CS ¡162 ¡ Intro ¡to ¡Computer ¡Science ¡II ¡ Separate ¡Compila4on ¡ 1 ¡

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

  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 ¡

  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 ¡

  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 ¡

  6. Example ¡ func4ons.cpp ¡ #include “functions.hpp” int square(int x) { return x * x; } func4ons.hpp ¡ int square(int x) 6 ¡

  7. Extended ¡Example ¡ program1.cpp ¡ #include “functions.hpp” int main() { std::cout << square(12) << std::endl; return 0; } 7 ¡

  8. Extended ¡Example ¡ Compile ¡with: ¡ ¡ G++ program1.cpp functions.cpp –o program1 Produces ¡an ¡executable ¡file ¡called ¡program1 ¡ ¡ 8 ¡

  9. Extended ¡Example ¡ To ¡create ¡an ¡object ¡file, ¡just ¡the ¡compila4on ¡of ¡ one ¡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 ¡

  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 ¡

  11. Guard ¡Example ¡ func4ons.hpp ¡ #ifndef FUNCTIONS_HPP #define FUNCTIONS_HPP int square(int x); #endif 11 ¡

  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 ¡ object-­‑oriented ¡programming! ¡ ¡ 12 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend