【发布时间】:2018-08-27 10:51:09
【问题描述】:
系统:Mac OS 10.12.6。 Python:来自 Anconda3 的 Python 3.5.2。 Cython==0.28。
我用
设置和编译 Cython# the .pyx file
from libc.stdint cimport *
cimport CLexActivator
def SetProductFile(filePath):
cdef bytes py_bytes = filePath.encode()
cdef const char* c_string = py_bytes
cdef int32_t status = CLexActivator.SetProductFile(c_string)
print(status)
return status
和
# the setup file
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
ext_modules=[
Extension("PyLexActivator",
sources=["PyLexActivator.pyx"],
language='c',
extra_objects=["libLexActivator.a"],
)
]
setup(
name = "PyLexActivator",
ext_modules = cythonize(ext_modules)
)
我使用python setup.py build_ext --inplace 编译。
Compiling PyLexActivator.pyx because it changed.
[1/1] Cythonizing PyLexActivator.pyx
running build_ext
building 'PyLexActivator' extension
creating build
creating build/temp.macosx-10.6-x86_64-3.5
/usr/bin/clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/o/anaconda/include -arch x86_64 -I. -I/Users/o/anaconda/include/python3.5m -c PyLexActivator.c -o
build/temp.macosx-10.6-x86_64-3.5/PyLexActivator.o
/usr/bin/clang -bundle -undefined dynamic_lookup -L/Users/o/anaconda/lib -arch x86_64 build/temp.macosx-10.6-x86_64-3.5/PyLexActivator.o libLexActivator.a -L/Users/o/anaconda/lib -o /path to/PyLexActivator.cpython-35m-darwin.so
运行import PyLexActivator时出错
dlopen(/path to/PyLexActivator.cpython-35m-darwin.so, 2):
Symbol not found: __ZNSs4_Rep20_S_empty_rep_storageE
Referenced from: /path to/PyLexActivator.cpython-35m-darwin.so
Expected in: flat namespace
in /path to/PyLexActivator.cpython-35m-darwin.so
我不知道__ZNSs4_Rep20_S_empty_rep_storageE 代表什么。由于.pyx 使用静态库libLexActivator.a 编译,我猜这个错误可能来自未知引用。但是不知道怎么解决。
我也用otool -L来展示
PyLexActivator.cpython-35m-darwin.so:
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1238.60.2)
PS:如果我使用language="c++",则会出现另一个错误Symbol not found: _kSCPropNetProxiesHTTPPort。
【问题讨论】:
-
那是
_std::string::_Rep::_S_empty_rep_storage的 C++ 名称修改。像这样的错误通常意味着您将一些代码编译为 C++,但随后没有与 C++ 标准库链接。在这种情况下,大概是libLexActivator.a需要 C++ 标准库。如果是这样,两个选项是:(1)添加language="c++"以便 Cython 将所有内容构建为 C++,或(2)添加适当的libc++或libstdc++或类似的作为额外的库。 -
嗨,@abarnert 当我使用
language="c++"时,还有另一个错误Symbol not found: _kSCPropNetProxiesHTTPPort。 -
这是一个不同的问题。
_kSCPropNetProxiesHTTPPort是CoreFoundation框架的一部分,因此您还需要链接 that。与其一步一步地尝试找出每个错误的来源,是否有一些关于针对libLexActivator.a构建可执行文件/dylib 的文档?如果没有,是否有 Makefile(或它使用的任何构建系统)? -
@abarnert 我认为你可能是对的。我已经在 Xcode 中成功使用了这个静态库。我添加了两个额外的库
CoreFoundation.framework和SystemConfiguration.framework。我在 Xcode 中使用了c++编译器。 -
好的,这正是您在 Cython 中需要做的事情。我很确定有一种正确的方法可以在
setup的Extension构造函数中指定框架(而不是假装它们只是奇怪的LDFLAGS),但我不记得它是什么。尝试搜索,如果找不到,我也可以尝试。
标签: python c++ c++11 clang cython