Fixes for C++ correctness

This is a set of changes that should improve compatibility with various C++ compilers, as well as one change for POSIX compatibility. The commits describe the specific issues.