Sc++ C++到Python:编译一组.cp和.h文件

2024-04-29 14:08:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个C++库,它由下面的CPP和H文件组成。我希望公开皮质.cpp到Python(3.5)。在

cortex.h
cortex_socket.h
cortex_intern.h
m3x3.h
cortex_unpack.h

m3x3.cpp
cortex_unpack.cpp  
cortex.cpp 
cortex_socket.cpp

我创建了以下皮层。我归档swig:

^{pr2}$

接下来,我使用以下代码编译模块:

swig -python -Isrc cortex.i
g++ -Isrc -fPIC -I/usr/include/python3.5 -c cortex.cpp cortex_wrap.c
g++ -shared -fPIC -o _cortex.so cortex.o cortex_wrap.o

这些命令不会返回错误。但是,尝试在python中使用生成的模块时,会弹出一个错误:

>>> import cortex
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "cortex.py", line 28, in <module>
    _cortex = swig_import_helper()
  File "cortex.py", line 24, in swig_import_helper
    _mod = imp.load_module('_cortex', fp, pathname, description)
ImportError: ./_cortex.so: undefined symbol: _Z13GetHostByAddrPhPc
>>> 

我在这个论坛上找到了与这个错误有关的其他几个问题/答案。然而,由于我不熟悉SWIG ANC C++,所以我很难把这些翻译成我自己的情况、代码和命令——换句话说:即使在阅读了其他的文章之后,我仍然迷失了方向。在

我从网上的各种来源收集了上面的代码片段。因此,如果有人向我解释这些代码行的作用(这可能有助于我找到解决方案),我也会非常感激。在

无论如何,有些帮助都会很好。在


Tags: 模块代码inimport错误linecortexsocket