日期:2014-05-16  浏览次数:20553 次

windows下编译python的evelDB库

本人使用的是python2.6, 32位,附上编译出来的dll , [不支持xp]

http://download.csdn.net/detail/toontong/4933540



 levelDB的官方库在这: http://code.google.com/p/leveldb/

但官方库是不能在win下编译过了,或是我没找到编译官方levelDB的方法,


1)在giyhub上有达人放出了可以在win编译的过的版本,以下使用的就是这个版本。

        [不支持xp, 代码中使用InitOnceExecuteOnce]

       https://github.com/chirino/leveldb/blob/master/WINDOWS.md

     diff过与官方的代码,确实有很多不一样的,其中官方有没修复某些bug就不得而已,风险自见。

通过以上方法可以编译出leveldb.lib文件。

       如果编译不过,应该是宏LEVELDB_PLATFORM_WINDOWS没定义,具体如何 加宏,使用vs界面加吧。


2)另外还要snapppy使用vs编译的工程:

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

此工程是vs2008的,可以转成vs2010的再编译过,得到文件:synapppy.lib

3)取出py-leveldb的代码:

      svn export http://py-leveldb.googlecode.com/svn/trunk

      修改 setup.py文件:

extra_compile_args = ['-I../leveldb-master/include','-I../snappy-1.0.5' '-fPIC', '-Wall', '-g2', '-D_GNU_SOURCE', '-O2', '-DNDEBUG', '-DLEVELDB_PLATFORM_WINDOWS']
extra_link_args = ['shlwapi.lib', 'snappy.lib','leveldb.lib',]

主要是头文件的包含目录与三个静态库文件,其中shlwapi.lib是系统自带的;

'snappy.lib','leveldb.lib'这个是上面1~2步编译出来的,把这两个文件copy在setup.py同级目录下,形如:



4)我安装的vs2010,因为python也是2008编译的,要修改C:\Python26\Lib\distutils\msvc9compiler.py 这个文件:

def find_vcvarsall(version):
    return r'C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat'
    """Find the vcvarsall.bat file

    At first it tries to find the productdir of VS 2008 in the registry. If
    that fails it falls back to the VS90COMNTOOLS env var.
    """
我直接修改find_vcvarsall函数,返回了vcvarsall.bat的路径。

       不这样修改会出现下面这个错:

C:\Python26\lib\site-packages\wx-2.9.4-msw
running build
running build_py
package init file '__init__.py' not found (or not a regular fil
package init file '__init__.py' not found (or not a regular fil
running build_ext
building 'leveldb' extension
error: Unable to find vcvarsall.bat
即使vcvarsall.bat所在目录已在环境变量中,也会报此错,更详细原因,请看:http://blog.csdn.net/toontong/article/details/8442681


最后还会有一个错:

build\temp.win32-2.7\Release\pcapy.pyd.manifest : general error c1010070:
 Failed to load and parse the manifest. The system cannot find the file specified.error:
 command 'mt.exe' failed with exit status 31

解决办法:由于vs2010的link.exe的参数稍微有些改变,所以在link的时候没有生成manifest文件,自然mt.exe找不到这个文件。只需要在msvc9compiler.py里面搜索一下MANIFESTFILE,然后在他上面加一行ld_args.append('/MANIFEST'),保存就OK了。[引自]

最后就是 python setup.py build

最后成功看到 leveldb.pyd