 examples/cpp/decode/file/main.cpp     |  5 +++++
 examples/cpp/encode/file/main.cpp     |  4 ++++
 include/FLAC/ordinals.h               |  3 +--
 include/share/alloc.h                 |  3 ++-
 include/share/compat.h                | 34 ++++++++++++++++++++++++++++++++++
 src/flac/analyze.c                    |  4 ++++
 src/libFLAC/include/private/bitmath.h |  1 +
 src/libFLAC/lpc.c                     |  4 ++++
 src/libFLAC/stream_decoder.c          |  4 ++++
 src/test_libFLAC++/metadata_manip.cpp |  6 ++++++
 src/test_streams/main.c               |  5 ++++-
 11 files changed, 69 insertions(+), 4 deletions(-)

diff --git a/examples/cpp/decode/file/main.cpp b/examples/cpp/decode/file/main.cpp
index b991d68..b978ea5 100644
--- a/examples/cpp/decode/file/main.cpp
+++ b/examples/cpp/decode/file/main.cpp
@@ -31,8 +31,13 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+
+#if HAVE_INTTYPES_H
 #define __STDC_FORMAT_MACROS
 #include <inttypes.h>
+#else
+#include "share/compat.h"
+#endif
 
 #include "FLAC++/decoder.h"
 
diff --git a/examples/cpp/encode/file/main.cpp b/examples/cpp/encode/file/main.cpp
index a83de05..2f83768 100644
--- a/examples/cpp/encode/file/main.cpp
+++ b/examples/cpp/encode/file/main.cpp
@@ -32,8 +32,12 @@
 #include <stdlib.h>
 #include <string.h>
 
+#if HAVE_INTTYPES_H
 #define __STDC_FORMAT_MACROS
 #include <inttypes.h>
+#else
+#include "share/compat.h"
+#endif
 
 #include "FLAC++/metadata.h"
 #include "FLAC++/encoder.h"
diff --git a/include/FLAC/ordinals.h b/include/FLAC/ordinals.h
index 647b07e..e48b8b5 100644
--- a/include/FLAC/ordinals.h
+++ b/include/FLAC/ordinals.h
@@ -35,8 +35,7 @@
 /* If your compiler does not provide <stdint.h> you should provide a replacement
  * which hss suitable replacements for the following intX_T and uintX_t types.
  */
-
-#include <stdint.h>
+#include "share/compat.h"
 
 typedef int8_t FLAC__int8;
 typedef uint8_t FLAC__uint8;
diff --git a/include/share/alloc.h b/include/share/alloc.h
index 7aa17f7..8fc17f7 100644
--- a/include/share/alloc.h
+++ b/include/share/alloc.h
@@ -45,11 +45,12 @@
 #include <stdint.h> /* for SIZE_MAX in case limits.h didn't get it */
 #endif
 #include <stdlib.h> /* for size_t, malloc(), etc */
+#include "share/compat.h"
 
 #ifndef SIZE_MAX
 # ifndef SIZE_T_MAX
 #  ifdef _MSC_VER
-#   define SIZE_T_MAX UINT_MAX /* What happens on 64 bit windows? */
+#   define SIZE_T_MAX SIZE_MAX
 #  else
 #   error
 #  endif
diff --git a/include/share/compat.h b/include/share/compat.h
index 983b045..4fa03cf 100644
--- a/include/share/compat.h
+++ b/include/share/compat.h
@@ -36,6 +36,9 @@
  * It is assumed that this header will be included after "config.h".
  */
 
+#ifndef FLAC__SHARE__COMPAT_H
+#define FLAC__SHARE__COMPAT_H
+
 #if defined _WIN32 && !defined __CYGWIN__
 /* where MSVC puts unlink() */
 # include <io.h>
@@ -65,7 +68,10 @@
 #endif
 
 #if defined(_MSC_VER)
+#if _MSC_VER < 1500
+/* Visual Studio 2008 has restrict. */
 #define restrict __restrict
+#endif
 #define inline __inline
 #endif
 
@@ -105,3 +111,31 @@
 #include <utime.h> /* for utime() */
 #include <unistd.h> /* for chown(), unlink() */
 #endif
+
+#if defined _MSC_VER
+#  if _MSC_VER >= 1600
+/* Visual Studio 2010 has decent C99 support */
+#    include <stdint.h>
+#    define PRIu64 "llu"
+#    define PRId64 "lld"
+#    define PRIx64 "llx"
+#  else
+#    include <limits.h>
+#    ifndef UINT32_MAX
+#      define UINT32_MAX _UI32_MAX
+#    endif
+     typedef unsigned __int64 uint64_t;
+     typedef unsigned __int32 uint32_t;
+     typedef unsigned __int16 uint16_t;
+     typedef unsigned __int8 uint8_t;
+     typedef __int64 int64_t;
+     typedef __int32 int32_t;
+     typedef __int16 int16_t;
+     typedef __int8  int8_t;
+#    define PRIu64 "I64u"
+#    define PRId64 "I64d"
+#    define PRIx64 "I64x"
+#  endif
+#endif /* defined _MSC_VER */
+
+#endif /* FLAC__SHARE__COMPAT_H */
\ No newline at end of file
diff --git a/src/flac/analyze.c b/src/flac/analyze.c
index 038ff32..dc8f473 100644
--- a/src/flac/analyze.c
+++ b/src/flac/analyze.c
@@ -25,7 +25,11 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#if HAVE_INTTYPES_H
 #include <inttypes.h>
+#else
+#include "share/compat.h"
+#endif
 #include "FLAC/all.h"
 #include "analyze.h"
 
diff --git a/src/libFLAC/include/private/bitmath.h b/src/libFLAC/include/private/bitmath.h
index 4e60f78..05daba1 100644
--- a/src/libFLAC/include/private/bitmath.h
+++ b/src/libFLAC/include/private/bitmath.h
@@ -36,6 +36,7 @@
 
 /* for CHAR_BIT */
 #include <limits.h>
+#include "share/compat.h"
 
 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
 #include <intrin.h> /* for _BitScanReverse* */
diff --git a/src/libFLAC/lpc.c b/src/libFLAC/lpc.c
index 66a6899..80e3564 100644
--- a/src/libFLAC/lpc.c
+++ b/src/libFLAC/lpc.c
@@ -34,7 +34,11 @@
 #endif
 
 #include <math.h>
+#if HAVE_INTTYPES_H
 #include <inttypes.h>
+#else
+#include "share/compat.h"
+#endif
 #include "FLAC/assert.h"
 #include "FLAC/format.h"
 #include "private/bitmath.h"
diff --git a/src/libFLAC/stream_decoder.c b/src/libFLAC/stream_decoder.c
index 789db1b..ec36510 100644
--- a/src/libFLAC/stream_decoder.c
+++ b/src/libFLAC/stream_decoder.c
@@ -55,7 +55,11 @@
 
 
 /* technically this should be in an "export.c" but this is convenient enough */
+#ifdef FLAC_API_SUPPORTS_OGG_FLAC
 FLAC_API int FLAC_API_SUPPORTS_OGG_FLAC = FLAC__HAS_OGG ;
+#else
+FLAC_API int FLAC_API_SUPPORTS_OGG_FLAC = 0 ;
+#endif
 
 
 /***********************************************************************
diff --git a/src/test_libFLAC++/metadata_manip.cpp b/src/test_libFLAC++/metadata_manip.cpp
index 1d75dca..3db9818 100644
--- a/src/test_libFLAC++/metadata_manip.cpp
+++ b/src/test_libFLAC++/metadata_manip.cpp
@@ -24,8 +24,14 @@
 #include <stdlib.h> /* for malloc() */
 #include <string.h> /* for memcpy()/memset() */
 #include <sys/types.h> /* some flavors of BSD (like OS X) require this to get time_t */
+#ifdef _MSC_VER
+#include <sys/utime.h>
+#else
 #include <utime.h> /* for utime() */
+#endif
+#if !defined _MSC_VER && !defined __MINGW32__ && !defined __EMX__
 #include <unistd.h> /* for chown(), unlink() */
+#endif
 #include <sys/stat.h> /* for stat(), maybe chmod() */
 #include "FLAC/assert.h"
 #include "FLAC++/decoder.h"
diff --git a/src/test_streams/main.c b/src/test_streams/main.c
index 45d8d21..0c87e7f 100644
--- a/src/test_streams/main.c
+++ b/src/test_streams/main.c
@@ -902,8 +902,11 @@ static FLAC__bool generate_noisy_sine(void)
 
 		sample += sin (2.0 * k * M_PI * 1.0 / 32.0);
 		sample *= 0.4;
-
+#if !defined _MSC_VER
 		write_little_endian_int16(f, lrintf(sample * 32700.0));
+#else
+		write_little_endian_int16(f, (FLAC__int16)(sample * 32700.0));
+#endif
 	};
 
 	fclose(f);
