Fixes issue#2288. Includes cleanups from Luke's pull 2243.
Tested on OSX, Linux, and cross-compiling in gitian.
"We" should refactor all of the common makefile code into a makefile.common, and include it from the other makefiles. There is an odd mix of CFLAGS / CXXFLAGS / xCXXFLAGS in our makefiles.