Building snappy compression library on windows with Visual Studio 2012

Snappy is a little fast software compression library by google. It eschews high compression rates for speed, it is very easy to use. Building it on windows for use with visual studio etc. is relatively straight-forward due to this github project by kmanley which provides it with a visual studio solution.

https://github.com/kmanley/snappy-msvc

To build the snappy static libraries, follow the instructions on the github page (README.rst).

If you get a build error like this:

.
Error	15	error C2061: syntax error : identifier 'ssize_t'
.

Then copy the following code and place it at the top of the snappy.h header file (say, after #define UTIL_SNAPPY_SNAPPY_H__):

//
#include 
typedef SSIZE_T ssize_t;
//

Once it has built, snappy.lib can be found in the Debug or Release folders, note that the debug build of the snappy lib is very slow so don’t do any tome testing with it! The release build screams along!

To use snappy in your C/C++ project you will need to #include “snappy.h” (which in-turn uses “snappy-stubs-public.h”) and also add snappy.lib to your project. Here is a little silly example:

//
#include 
#include "snappy.h"

void test_compress() {

	std::string in("This is not a very long string at all!!!  So the compressed output may be larger than its size, but don't worry it will work well for longer things");

	printf("Abut to compress string (size is %d)\n", in.size());

	// 
	// Compress the string...
	//
	std::string out;
	auto comp_size = snappy::Compress(in.c_str(), in.size(), &out);
	
	printf("Compressed size is %d\n", comp_size);

	//
	// Uncompress it..
	//
	std::string plain;
	snappy::Uncompress(out.data(), comp_size, &plain);
	printf("Un-compressed size is %d\n", plain.size());

	printf(plain.c_str());
}
//

If you get an error like this when you try to compile your client code:

.
Error	1	error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '2' doesn't match value '0' 
.

Then it means you are trying to use the debug version of snappy.lib in a release build or vice versa, use the correct version and the error should go away….

Many thanks to kmanley!