简体   繁体   中英

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. I wish to expose the functions in cortex.cpp to 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:

%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:

>>> 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.

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至关重要。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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