我有一个简单的.proto,如下所示:
syntax = "proto2";
package p2p;
message dataFrame {
repeated int64 row_label = 1;
repeated int64 col_label = 2;
message data_frame_row {
repeated int32 column = 1;
}
repeated data_frame_row row = 3;
}
我使用protoc进行编译。
我可以编译并使用以下(简化的)cpp代码(transitMatrix.h #includes“ p2p.pb.h”):
#include "transitMatrix.h"
int main()
{
transitMatrix matrix(5, true);
matrix.addEdgeToGraph(0, 1, 5, true);
matrix.addEdgeToGraph(1, 2, 6, true);
matrix.writeTMX("outfile.tmx");
return 0;
}
但是,transitMatrix类旨在通过distutils进行cythonized。
在我的(经过简化的)test.pyx文件中,我用cdef替换了transitMatrix类和dataFrame类:
cdef extern from "src/utils/serializer/p2p.pb.cc" namespace "p2p":
cdef cppclass dataFrame:
dataFrame()
cdef extern from "src/transitMatrix.h":
cdef cppclass transitMatrix:
transitMatrix()
在我的setup.py中,distutils扩展如下:
EXTENSION = distutils.extension.Extension(
name = 'test', language = 'c++',
sources = ['test.pyx'],
extra_compile_args = ['-Wno-unused-function',
'-std=c++11', '-Wall', '-O3', '-lprotobuf'
] + ouff_mac,
undef_macros = ["NDEBUG"],
extra_link_args = ouff_mac
)
当我运行python3 setup.py install时,它会成功,但会发出警告:
clang: warning: -lprotobuf: 'linker' input unused
最后,当我尝试在iPython(transitMatrixAdapter)中导入生成的类时,出现以下错误:
ImportError: dlopen(/Users/user/anaconda3/lib/python3.6/site- packages/transitMatrixAdapter.cpython-36m-darwin.so, 2): Symbol not found: __ZN6google8protobuf7Message20DiscardUnknownFieldsEv
Referenced from: /Users/user/anaconda3/lib/python3.6/site-packages/transitMatrixAdapter.cpython-36m-darwin.so
Expected in: flat namespace in /Users/user/anaconda3/lib/python3.6/site-packages/transitMatrixAdapter.cpython-36m-darwin.so
我应该补充一点,即在删除protobuf依赖项时,此功能可以按预期工作。为什么找不到.so?我认为这是一个链接器错误,但是问题出在哪里?