MinGW compatibility.

Originally from alex85k.
Add missing check for Threads lib to cmake config.
cmake files: Changed compile options and some cosmetic changes.