c++ and unix makefiles and shell scripts

.. 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 exefile How do we make it easy? shell script #################################################################### # 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