• 2012-11-10

    Compiling levelDB on Windows Cygwin

    Views: 16402 | 1 Comment

    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

    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

    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:


    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
    Posted by ideawu at 2012-11-10 12:09:50 Tags: ,
|<<<1>>>| 1/1 Pages, 1 Results.