简体   繁体   English

Swig C ++ to python:编译一堆.cpp和.h文件

[英]Swig C++ to python: compiling a bunch of .cpp and .h files

I have a C++ library that consists of the following cpp and h files. 我有一个C ++库,包含以下cpp和h文件。 I wish to expose the functions in cortex.cpp to Python (3.5). 我希望将cortex.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

I create the following cortex.i file swig: 我创建了以下cortex.i文件swig:

%module cortex
%{
#include "cortex.h"
#include "m3x3.h"
#include "cortex_intern.h"
#include "cortex_socket.h"
#include "cortex_unpack.h"
#include "stdbool.h"
%}

%include "cortex.h"

Next, I use the following to compile the module: 接下来,我使用以下代码来编译模块:

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

These commands do not return an error. 这些命令不会返回错误。 However, when trying to use the resulting module in python, an error pops up: 但是,当尝试在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
>>> 

I have found several other questions/answers on this forum relating to this error. 我在这个论坛上发现了与此错误有关的其他几个问题/答案。 However, as am not familiar with swig anc C++ it's difficult for me to to translate these to my own situation, code and commands - in other words: even after reading these other posts, I am still lost. 但是,由于我不熟悉swig和C ++,我很难将这些翻译成我自己的情况,代码和命令 - 换句话说:即使阅读了这些其他帖子后,我仍然迷失了。

I've collected the snippets of code above from various sources on the web. 我从网上的各种来源收集了上面的代码片段。 Hence, I would also be grateful if someone explained me what these lines of code do (which might help me finding a solution). 因此,如果有人向我解释了这些代码行的作用(这可能有助于我找到解决方案),我将不胜感激。

In any case, some help would be great. 无论如何,一些帮助会很棒。

在包装C ++代码时,使用`-c ++'选项调用SWIG至关重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM