在Snow Leopard上为Python编译Matplotlib

20 投票
7 回答
9961 浏览
提问于 2025-04-15 14:35

我花了半天时间想在Snow Leopard上编译matplotlib这个库,但一直没成功。我在网上找到了一个有用的页面(http://blog.hyperjeff.net/?p=160),但是还是无法编译成功。我看到其他用户在那个页面上也有评论,所以我知道我不是一个人在挣扎。

我已经单独安装了zlib、libpng和freetype这些库。

我把make.osx文件的开头改成了这样:

PREFIX=/usr/local

PYVERSION=2.6
PYTHON=python${PYVERSION}
ZLIBVERSION=1.2.3
PNGVERSION=1.2.33
FREETYPEVERSION=2.3.5
MACOSX_DEPLOYMENT_TARGET=10.6

## You shouldn't need to configure past this point

PKG_CONFIG_PATH="${PREFIX}/lib/pkgconfig"
CFLAGS="-Os -arch x86_64 -arch i386 -I${PREFIX}/include"
LDFLAGS="-arch x86_64 -arch i386 -L${PREFIX}/lib"
CFLAGS_DEPS="-arch i386 -arch x86_64 -I${PREFIX}/include -I${PREFIX}/include/freetype2 -isysroot /Developer/SDKs/MacOSX10.6.sdk"
LDFLAGS_DEPS="-arch i386 -arch x86_64 -L${PREFIX}/lib -syslibroot,/Developer/SDKs/MacOSX10.6.sdk"

然后我运行了:

sudo make -f make.osx mpl_build

结果是:

export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig" &&\
    export MACOSX_DEPLOYMENT_TARGET=10.6 &&\
    export CFLAGS="-Os -arch x86_64 -arch i386 -I/usr/local/include" &&\
    export LDFLAGS="-arch x86_64 -arch i386 -L/usr/local/lib" &&\
    python2.6 setup.py build

... snip ...

gcc-4.2 -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -Os -arch x86_64 -arch i386 -I/usr/local/include -pipe -DPY_ARRAYAUNIQUE_SYMBOL=MPL_ARRAY_API -I/Library/Python/2.6/site-packages/numpy/core/include -I. -I/Library/Python/2.6/site-packages/numpy/core/include/freetype2 -I./freetype2 -I/System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6 -c src/ft2font.cpp -o build/temp.macosx-10.6-universal-2.6/src/ft2font.o
cc1plus: warning: command line option "-Wstrict-prototypes" is valid for C/ObjC but not for C++
In file included from src/ft2font.h:13,
                 from src/ft2font.cpp:1:
/usr/local/include/ft2build.h:56:38: error: freetype/config/ftheader.h: No such file or directory

... snip ...

src/ft2font.cpp:98: error: ‘FT_Int’ was not declared in this scope
/Library/Python/2.6/site-packages/numpy/core/include/numpy/__multiarray_api.h:1174: warning: ‘int _import_array()’ defined but not used
lipo: can't open input file: /var/tmp//ccDOGx37.out (No such file or directory)
error: command 'gcc-4.2' failed with exit status 1
make: *** [mpl_build] Error 1

我现在完全搞不清楚该怎么办了。

7 个回答

3

你也可以通过以下方式来构建:

$ python setup.py build

并且需要在setupext.py文件上应用以下补丁:

Index: setupext.py
===================================================================
--- setupext.py (revision 7917)
+++ setupext.py (working copy)
@@ -334,6 +334,8 @@

     module.include_dirs.extend(incdirs)
     module.include_dirs.append('.')
+    module.include_dirs.append('/usr/local/include')
+    module.include_dirs.append('/usr/local/include/freetype2')
     module.library_dirs.extend(libdirs)

 def getoutput(s):
6

这个解决方案在我的OSX 10.8.3上有效:

ln -s /usr/local/include/freetype2/freetype/ /usr/include/freetype

真正的功劳归于:http://simpleyuan.blogspot.com/2012/08/matplotlib-error-mac-os-x.html

7

根据你的错误信息,似乎缺少freetype的头文件。你可以试着用系统的搜索功能找到它们。我不会在这里讲解怎么使用现成的包,因为我也喜欢自己动手从头开始编译。

撰写回答