38 compile many files
-
header file:declaration : .h, have struct and function name
-
implementation file, .cpp, inside cpp must include header file, only have function
-
main file: calling, .cpp inside cpp must include header file, only have main
// file name: math_stuff.cpp #include "math_utils.h" double area(double length, double width) { return length * width; } double area(double length) { return length * length; } double area(Rectangle rectangle) { return rectangle.length * rectangle.width; } double pow(double base, int pow) { int total = 1; for(int i = 0; i < pow; i++) { total *= base; } return total; } // file name: math_utils.cpp #include "math_utils.h" double area(double length, double width) { return length * width; } double area(double length) { return length * length; } double area(Rectangle rectangle) { return rectangle.length * rectangle.width; } double pow(double base, int pow) { int total = 1; for(int i = 0; i < pow; i++) { total *= base; } return total; } // file name: math_utils.h #ifndef MATH_UTLIS #define MATH_UTLIS struct Rectangle { double length; double width; }; double area(double length, double width); double area(double length); double area(Rectangle rectangle); double pow(double base, int pow = 2); #endif // linux: compile // method1: // g++ math_stuff.cpp math_utils.cpp // ./a.out // method2: can complie each intermeidate step // g++ -c math_stuff.cpp math_utils.cpp // g++ math_stuff.o math_utils.o // ./a.out