100 likes | 290 Views
Principles of Scientific Programming. Tarik Roukny Yves Dominici. C++ and Software Programming. Chapter 6 : Source Files. Outline. Chap 1 – Introduction Chap 2 – Compiler and Memory Chap 3 – Types Chap 4 – Control Structures Chap 5 – Functions Chap 6 – Source Files
E N D
Principles of Scientific Programming Tarik Roukny Yves Dominici
C++ and Software Programming Chapter 6 : Source Files
Outline • Chap 1 – Introduction • Chap 2 – Compiler and Memory • Chap 3 – Types • Chap 4 – Control Structures • Chap 5 – Functions • Chap 6 – Source Files • Chap 7 – Classes and Objects
Multiple Compiling • Issue: • Programs can easily become series of very long lines of code • Separate the code in several files = modules • Advantage: • More easy to read • More flexible and extensible • Every modification only calls for the compiling of one file • Disadvantage: • Compiler needs to map all information • Need to be rigorous in the methodology
Multiple Compiling • The compiling process can now be separated in two steps • Compile code generation for each source code taken separately • Link editing In order to allow communication between the different parts/modules of the program
Multiple Compiling • Idea • Code separeted in several files (‘.cpp’) file1.cpp file2.cpp file3.cpp • Compile each file independently g++ -c file1 file1.cpp g++ -c file2 file2.cpp g++ -c file3 file3.cpp • Edit the links g++ -o finalProgram file1.o file2.o file3.o
Multiple Compiling • Design the structure w.r.t. to the logic of the program • 1 file for each specific tasks –module • 1 file for the main
Headers “.h” • How to communicate the link between the files: • Headers : files with ending “.h” • Import the information via : #include “headerName.h” • Simple copy-paste of the file • Like when importing a library • The header contains: • Declaration of functions accessible from the other modules • Definition of types used in the module module1.h #include “module1.h” #include “module1.h” module1.cpp main.cpp
Multiple includes • A header can include other headers • Problem • If a header includes a header that is including a header that includes the first header, etc. • Solution • Start with #ifndef NAME #define NAME • End with #endif /*NAME*?
10 Exercise: ATM Example No Yes No Yes No Yes • Extension: • Modularize using several files Yes No 11/8/2014