Cython 编译正常,但找不到符号:__ZNSs4_Rep20_S_empty_rep_storageE when 运行 on Mac OS

Cython compiled ok, but Symbol not found: __ZNSs4_Rep20_S_empty_rep_storageE when running on Mac OS

系统:Mac OS 10.12.6。 Python:Python 来自 Anoconda3 的 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

首先,缺少的函数是一个 C++ 损坏的名称。现在大多数编译器都带有 demangle 工具,或者您可以使用像 this one 这样的在线 demangler,它适用于所有 not-too-ancient 版本的 clang++、g++ 和 MSVC。结果是 _std::string::_Rep::_S_empty_rep_storage.

这显然是 C++ 标准库的一部分。问题是您编译了一些代码以使用 C++ std::string,但还没有 linked C++ stdlib。这不是您的 Cython-generated 代码,也不是 Python,所以大概是 libLexActivator.a.

解决此问题的简单方法是 have Cython compile all of your code as C++ instead of C,方法是在 cythonize 调用中添加 language="c++"。这比您需要的稍微极端一点,但可能没问题。

或者,您可以只选择正确的 C++ stdlib 和 link。这与 Mac 上的 clang 有点混淆,因为它们有两个,libc++libstdc++。最近的版本默认为前者(libc++ 是一个较新的实现,由 LLVM/Clang 团队构建,以更好地与 C++11 及更高版本一起工作)。但是如果你正在为 10.6 构建,我不确定这是否仍然正确。因此,您可能需要对其进行研究(或询问 C++、Mac 和 Clang 相关标签方面的专家),或者两者都尝试看看。


根据您的评论,在您修复该问题后,您会得到另一个缺失的符号,_kSCPropNetProxiesHTTPPort。这种独特的命名风格几乎可以肯定意味着它是由 Apple 的 CoreFoundation 框架内部或位于它之上的其他 C 框架之一导出的常量。但是不要猜测,只需将它粘贴到您最喜欢的搜索引擎中,您就会找到 the docs,这表明它是 SystemConfiguration 框架的一部分。因此,您还需要将其添加到构建中。

此时,很明显 libLexActivator.a 不仅仅是简单的 C/POSIX 代码;它有一些平台依赖性(据我所知,它可能也有 third-party 依赖性)需要 link 反对。因此,最好的办法是找到它的文档并查看 linking 需要什么。 (如果没有这样的文档,你应该能够从 Makefile and/or 其他构建工具中弄清楚,但如果你不知道如何,你真的应该问 separately-tagged C++ 帮助的问题。)