Fixes to build on gcc 4.8.1. Reorder of some libs

GCC 4.8.1 (in ubuntu/mint) is more picky about ordering of libs
for linking.