undefine macros and reorder header includes to get rid of system defined macros

fixes #1165