我正在尝试编译Cython代码并将其用作Python模块。我跟着Cython's basic tutorial。我发了文件hellowolrd.pyx:
print "Hello World"
我做了setup.py:
from distutils.core import setup
from Cython.Build import cythonize
setup(
ext_modules = cythonize("helloworld.pyx")
)
并运行:
python setup.py build_ext --inplace
但不幸的是发生了一个错误:
running build_ext
building 'helloworld' extension
gcc -pthread -Wno-unused-result -Wsign-compare -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DNDEBUG -O2 -g -pipe -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 -g -O2 -g -pipe -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 -I/usr/include/ncursesw -fPIC -I/usr/include/python3.5m -c helloworld.c -o build/temp.linux-x86_64-3.5/helloworld.o
gcc -pthread -shared -Wl,--as-needed -Wl,-z,relro -Wl,-O1 -Wl,--build-id -Wl,--enable-new-dtags build/temp.linux-x86_64-3.5/helloworld.o -L/usr/lib64 -lpython3.5m -o /home/dima/Projects/AntennaModeling/test/helloworld.cpython-35m-x86_64-linux-gnu.so
/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-mageia-linux-gnu/5.4.0/../../../libpython3.5m.so when searching for -lpython3.5m
/bin/ld: skipping incompatible /lib/libpython3.5m.so when searching for -lpython3.5m
/bin/ld: skipping incompatible /usr/lib/libpython3.5m.so when searching for -lpython3.5m
/bin/ld: cannot find -lpython3.5m
collect2: error: command ld failed with exit status 1
error: command 'gcc' failed with exit status 1
似乎gcc对python3.5库不满意。我尝试使用以下命令编译而没有distutils:
cython -a helloworld.pyx
gcc -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing -I/usr/include/python3.5m -o helloworld.so helloworld.c
它有效。从知道我可以从Python解释器导入helloworld。但我无法弄清楚如何使用distutils编译Cython代码。我试图使用extra_compile_args传递相同的标志但它没有用。
UPD: 它发现gcc找不到64位的python库,所以试图明确地显示库目录。所以我改变了setup.py
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
extensions = [
Extension("helloworld", ["helloworld.pyx"],
library_dirs = ['/usr/lib64']),
]
setup(
name = 'Hello world app',
ext_modules = cythonize(extensions),
)
仍然是相同的输出
running build_ext
building 'helloworld' extension
gcc -pthread -Wno-unused-result -Wsign-compare -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DNDEBUG -O2 -g -pipe -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 -g -O2 -g -pipe -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 -I/usr/include/ncursesw -fPIC -I/usr/include/python3.5m -c helloworld.c -o build/temp.linux-x86_64-3.5/helloworld.o
gcc -pthread -shared -Wl,--as-needed -Wl,-z,relro -Wl,-O1 -Wl,--build-id -Wl,--enable-new-dtags build/temp.linux-x86_64-3.5/helloworld.o -L/usr/lib64 -L/usr/lib64 -lpython3.5m -o /home/dima/Projects/AntennaModeling/test/helloworld.cpython-35m-x86_64-linux-gnu.so
/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-mageia-linux-gnu/5.4.0/../../../libpython3.5m.so when searching for -lpython3.5m
/bin/ld: skipping incompatible /lib/libpython3.5m.so when searching for -lpython3.5m
/bin/ld: skipping incompatible /usr/lib/libpython3.5m.so when searching for -lpython3.5m
/bin/ld: cannot find -lpython3.5m
collect2: error: command ld failed with exit status 1
error: command 'gcc' failed with exit status 1
答案 0 :(得分:1)
通常,链接器会搜索名称为libXXX.so
而不是libXXX.so.1.0
的库(如果不成功,则搜索名称为libXXX.a
的库)。通常libXXX.so
是libXXX.so.Y.0
的符号链接,它是真正的库。
您可以通过使用-Wl,--verbose
调用link-command来查看链接器,例如:
gcc -Wl,--verbose -pthread -shared -Wl,--as-needed -Wl,-z,relro -Wl,-O1 -Wl,--build-id -Wl,--enable-new-dtags build/temp.linux-x86_64-3.5/helloworld.o -L/usr/lib64 -lpython3.5m -o /home/dima/Projects/AntennaModeling/test/helloworld.cpython-35m-x86_64-linux-gnu.so
你会看到类似的东西:
...
attempt to open /usr/lib64/libpython3.5m.so failed
...
所以有两种选择:
答:在你的系统上引入符号链接/usr/lib64/libpython3.5m.so
。
或更少侵入性B:将libraries=[':libpython3.5m.so.1.0']
添加到您的扩展名定义中:
extensions = [
Extension("helloworld", ["helloworld.pyx"],
library_dirs = ['/usr/lib64']),
libraries=[':libpython3.5m.so.1.0']
]
现在,链接命令应该看起来有点不同:
gcc ... -L/usr/lib64 -l:libpython3.5m.so.1.0 ...
链接器将查找libpython3.5m.so.1.0
,而不仅仅是libpython3.5m.so
仍由distutils提供。