INTRO TO COMPUTER SCIENCE II
FILE SEPARATION & MAKEFILES
CS162
File Separation – What?
Different file types we can use
Interface file (.h)
Sometimes called header file
Sometimes has .hpp or no extension
Tells the world what your code components can do
Implementation file (.cpp)
Same name as the paired interface file
Actually does the work promised in the interface file
Driver file (.cpp) Unique program
Demonstrates functionality
Also sometimes known as “client code”
File Separation – When?
Different ways to separate and group files By objects
book.cpp/.h
struct book{}
print_book_info()
create_book_array()
By common functionality math.cpp/.h
multiply() divide()
scale()
File Separation – Where?
Different content goes in each file
Interface file (.h)
Describes structs, and function prototypes (name, arguments) Does not contain variable definitions, just declarations
Implementation file (.cpp) Paired w/same name as “.h” file
Contains all of the functions listed in the header file Include paired .h file
Driver file (.cpp)
contains “main” function, and any library files needed
REVIEW:
Declaration
– A variable or function can be declared any number of times – Memory is not allocated
– int f(int);
Definition
– A variable or function
can be defined once
– Memory is allocated
– int f(int a){
return a; }
File Separation – Why?
Programs can get very large making them difficult to navigate Makes aspects of the program more portable to other programs
use “math”, etc in other programs Easier to update/change code
If you change function prototype, then you don’t have to change it in main; automatically updated by header @ runtime
Example:
File Separation – More on Headers
#include
< > for files that come with the compiler #include
“ “ for files for any other header files #include “book.h” What happens if you try to declare the same variable or struct more
than once?
//book.h
struct book {
};
//collection.h
#include “book.h”
#include “movie.h”
struct collection {
};
//prog.cpp
#include “book.h”
#include “collection.h”
int main{ }
File Separation – Header Guards
Conditional compilation directives
Standard strategy in header files
Safeguards against compiler errors and redefining structs
//book.h
#ifndef BOOK_H
#define BOOK_H
struct book {
… };
#endif
Header Files
Best Practices
Always use header guards
Don’t define variables and functions, only declare Use same name as source .cpp file
Every header file should compile on it’s own
Include all dependencies needed Don’t include .cpp files
File Separation – Demo
//book.h
#include
struct book { int pages;
//book.cpp
#include
// simple function to print the book info
void print_book_info(book* bk) { }
// function that returns a pointer to a 2D array o f books
book** create_2d_array(int rows, int cols) {
}
//prog.cpp
#include
int main() { }
unsigned int pub_date; std::string title; unsigned int num_authors; std::string* authors;
// function prototypes
void print_book_info(book*); book** create_2d_array(int, int);
};
Makefiles
Special Unix utility named “make”
Executes shell commands contained in Makefile Can have multiple rules and commands General structure
target: dependency1 dependency2 …
system command
More info: http://www.sis.pitt.edu/mbsclass/tutorial/advanced/makefile/whatis.htm
Makefiles – Structure
comment executable
default target target
Using a variable $(VAR)
# Example makefile
CC = g++
exe_file = book_app
$(exe_file): book.o prog.o
compiler
dependencies (pre-requisites)
$(CC) book.o prog.o -o $(exe_file)
book.o: book.cpp
$(CC) -c book.cpp
prog.o: prog.cpp
$(CC) -c prog.cpp
clean:
rm -f *.out *.o $(exe_file)
recipe
Command line output when you type “make”:
g++ -c book.cpp
g++ -c prog.cpp
g++ book.o prog.o –o book_app
Makefiles
Tabs are important!
Can stop the compilation process
at any point
Compilation process:
Preprocessor/Expand macros
Compile C++/Translates to Assembly
Assembler runs/Translates to Machine Code
Linker runs/Translates to executable Compiles only what it needs to