Avoid repetition in the Makefile and automate header file dependencies

* Use pattern rules and header detection to eliminate repetition
* Added .gitignore to avoid accidental check-in of binaries and intermediate files