windows下編譯python的levelDB庫

本人使用的是python2.6, 32位,附上編譯出來的dll , [不支持xp]

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


支持xp的下載:http://download.csdn.net/detail/toontong/4938865


 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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章