The official levelDB does not support Cygwin platform, trying to compile it on Cygwin may get the error message:”Unknow platform”. Anyway, levelDB works on Windows Cygwin environment, all you have to do is to manually modify some files.
First, make sure your Cygwin has gcc-4 installed, not gcc-3:
$ gcc -v
gcc version 4.5.3 (GCC)
Then, edit build_detect_platform under levelDB source code folder, make it looks like this:
case "$TARGET_OS" in
CYGWIN_*)
PLATFORM=OS_LINUX
COMMON_FLAGS="$MEMCMP_FLAG -lpthread -DOS_LINUX -DCYGWIN"
PLATFORM_LDFLAGS="-lpthread"
PORT_FILE=port/port_posix.cc
;;
The “CYGWIN_*” codes is added. Then edit port/port_posix.h, make it looks like this:
#if defined(OS_MACOSX) || defined(OS_SOLARIS) || defined(OS_FREEBSD) ||\
defined(OS_NETBSD) || defined(OS_OPENBSD) || defined(OS_DRAGONFLYBSD) ||\
defined(OS_ANDROID) || defined(OS_HPUX) || defined(CYGWIN)
// Use fread/fwrite/fflush on platforms without _unlocked variants
#define fread_unlocked fread
#define fwrite_unlocked fwrite
#define fflush_unlocked fflush
#endif
The ” || defined(CYGWIN)” is added, or you may got this error if you try to run make:
util/env_posix.cc:50: error: `fread_unlocked' undeclared (first use this function)
Now, you can compile levelDB on Windows Cygwin:
make
If you can find the file libleveldb.a in levelDB source code folder, it’s sucessfull. levelDB is just a library, not a standalone server, so we write a simple CPP code example to test it:
#include <iostream>
#include "leveldb/db.h"
int main(){
leveldb::DB* db;
leveldb::Options options;
leveldb::Status status;
options.create_if_missing = true;
status = leveldb::DB::Open(options, "./testdb", &db);
std::string key = "abc";
std::string value = "123";
std::string ret;
std::cout << value;
status = db->Put(leveldb::WriteOptions(), key, value);
status = db->Get(leveldb::ReadOptions(), key, &ret);
std::cout << ret;
return 0;
}
Compile this code(file named a.cpp) and run with command:
g++ a.cpp -Iinclude -L. -lleveldb; ./a