c++ functions in files

Developing large projects with many people creating many functions necessitates mechanisms that allow those functions to be devoloped separately.

The most difficult part, however, is 'integration and test' - seeing if all the functions can be made to work together - correctly...



.. functions in separate files

See example on jupiter ------------------------------------------- Why separate files for each function? modularization for understanding ease of development ease of testing re-use multiple person development geographic separation development
Example 1a: ONE FILE PROGRAM : 2 functions ========= (CC main.cpp) #include <iostream> using namespace std; void out( int ); // prototype // main function int main( ) { int num;; cin >> num; out( num ); return 0; } // out function void out( int number ) { cout << number << endl; return; }
Example 2: TWO FILE PROGRAM : 2 functions ========== (CC main.cpp out.cpp) main.cpp ======== #include <iostream> using namespace std; void out( int ); // prototype // main function int main( ) { int num;; cin >> num; out( num ); return 0; } out.cpp ======= #include <iostream> using namespace std; // out function void out( int number ) { cout << number << endl; return; }
Example 3: ONE FILE PROGRAM : 2 functions, global struct ========= (CC letters.cpp -o letter) #include <iostream> using namespace std; struct letterType // globally defined { char letter; int count; }; void out( letterType ); // prototype // main function int main( ) { letterType aLetter[5]; cin >> aLetter[0].letter >> aLetter[0].count; out( aLetter[0] ); return 0; } // out function uses struct declared globally void out( letterType let ) { cout << let.letter << let.count; return; }

How would you BREAK THIS UP?


.. header(.h) files: sharing data

Header Files (.h) ================= A separate file with data definitions that can be shared by multiple files in a project. Single definition Easier to update / maintain
Example: THREE FILE PROGRAM : 2 functions, 1 include file ======= ( CC main.cpp out.cpp -o letter ) // FILE: project.h // define struct in project.h, then each .cpp file can include it struct letterType // globally defined { char letter; int count; }; ------------------------------ // FILE: main.cpp #include <iostream> #include "project.h" // <<==== Syntax!! void out( letterType ); using namespace std; int main( ) { letterType aLetter[5]; cin >> aLetter[0].letter >> aLetter[0].count; out( aLetter[0] ); system( "pause" ); return 0; } ------------------------------ // FILE: out.cpp // function uses struct #include <iostream> using namespace std; #include "project.h" void out( letterType let ) { cout << let.letter << let.count; return; }

.. separate compilation: shell scripts

Why separate files for each function? modularization for understanding ease of development ease of testing re-use multiple person development geographic separation development
Document!! For Users of the function(s): EXPLAIN what it does... name of function parameters: types and order - NOT names return type For coders that may modify your function doc the algorithm doc the code
How do we compile multiple functions?? CC CC file1.cpp file2.cpp file3.cpp -o exec How do we automate? shell script Note: '#' is a comment # # Shell script compiles multiple files into executable called exec # Remember to make it executable! (chmod 705 execCC) # To execute this script, type the name of THIS file (execCC) # CC file1.cpp file2.cpp file3.cpp -o exec # # remove the object files generated when compiling rm file*.o
EXAMPLE: ======== #################################################################### # This is a shell script that compiles multiple files # into a single executable called sudoku # Be executable! (chmod 705 filename) # To execute, type the name of this file (sudokuCompile) # # compile and link all the 4 modules into an exe CC sudoku.cpp initGrid.cpp rowColGrid.cpp simpleGrid.cpp -o sudoku # # remove the object files generated when compiling rm *.o #################################################################### Use each time to compile, BUT would ALWAYS compile EVERYTHING [see makefiles for a better way] Use pico to create the script file (pico sudokuCompile) Use chmod so it's executable (chmod 705 sudokuCompile) ***

.. make and separate compilation: make and makefiles

makefile and make ================= The make utility uses a makefile to compile and link your executable. It has dependencies: list of the files that make it up if ANY of these files change , it re-compiles those files and re-links It has rules: what to do simply the CC command to execute $ make (this command uses the makefile called "makefile" (below) by default) All dependcy changes based on time Use "touch" to chamge time stamp on file and force re-compile or re-link
EXAMPLE makefile: (pico makefile) ================= ########################################################## # # this is a simple makefile for a "helloworld" c++ program # # to create hello (the exe), the only dependency is hello.cpp hello: hello.cpp CC hello.cpp -o hello # note that the command (CC) MUST be indented with **TAB** # EXAMPLE makefile 2: (pico makefile) ================= ########################################################## # # this is a simple makefile for a "helloworld" c++ program # # to create hello (the exe), the only dependency is hello.cpp hello: hello.o CC hello.o -o hello hello.o: CC -c hello.cpp # note that the command (CC) MUST be indented with **TAB** # ^^^
NOTE: \ to continue line NOTE: TAB in for the CC action or ANY action...
EXAMPLE makefile: ================= ################################################################ # makefile for sudoku # # Dependency List: # ================ # the exe, sudoku (target) depends on ALL these files (include .h) # note the "space\" line continuation # word wrap may cause issues # sudoku: sudokuMain.cpp initGrid.cpp printing.cpp rowColGrid.cpp \ rowGrid.cpp simpleGrid.cpp sudokuGrid.cpp header.cpp \ makePuzzle.cpp sudoku.h # # Action List: # ============ # the action to initiate if out of date.. (USE TAB!) # CC -g -o sudoku sudokuMain.cpp initGrid.cpp \ printing.cpp rowColGrid.cpp rowGrid.cpp \ simpleGrid.cpp sudokuGrid.cpp header.cpp makePuzzle.cpp
jupiter|/export/home/wyatt/private/Code/makeExample$ make CC sudoku.cpp initGrid.cpp printing.cpp rowColGrid.cpp rowGrid.cpp simpleGrid.cpp sudokuGrid.cpp -o sudoku sudoku.cpp: initGrid.cpp: printing.cpp: rowColGrid.cpp: rowGrid.cpp: simpleGrid.cpp: sudokuGrid.cpp:
################################################################ # REALLY USEFUL makefile for sudoku # only compile and link as needed # per individual file changes! # # Dependency List: # ================ # the exe, sudoku (target) depends on these OBJECT files # sudoku: sudokuMain.o initGrid.o printing.o rowColGrid.o \ rowGrid.o simpleGrid.o sudokuGrid.o header.o \ makePuzzle.o # # Action List: the action to create EXE from .o files (USE TAB!) # CC -o sudoku sudokuMain.o initGrid.o \ printing.o rowColGrid.o rowGrid.o \ simpleGrid.o sudokuGrid.o header.o makePuzzle.o # # Now create EACH .o as NEEDED # this ONLY creates files based on what have been changed # sudokuMain.o: sudokuMain.cpp sudoku.h CC -c sudokuMain.cpp initGrid.o: initGrid.cpp sudoku.h CC -c initGrid.cpp printing.o: printing.cpp sudoku.h CC -c printing.cpp rowColGrid.o: rowColGrid.cpp sudoku.h CC -c rowColGrid.cpp rowGrid.o: rowGrid.cpp sudoku.h CC -c rowGrid.cpp simpleGrid.o: simpleGrid.cpp sudoku.h CC -c simpleGrid.cpp sudokuGrid.o: sudokuGrid.cpp sudoku.h CC -c sudokuGrid.cpp header.o: header.cpp sudoku.h CC -c header.cpp makePuzzle.o: makePuzzle.cpp sudoku.h CC -c makePuzzle.cpp