簡體   English   中英

Swig C ++ to python:編譯一堆.cpp和.h文件

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

我有一個C ++庫,包含以下cpp和h文件。 我希望將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

我創建了以下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"

接下來,我使用以下代碼來編譯模塊:

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和C ++,我很難將這些翻譯成我自己的情況,代碼和命令 - 換句話說:即使閱讀了這些其他帖子后,我仍然迷失了。

我從網上的各種來源收集了上面的代碼片段。 因此,如果有人向我解釋了這些代碼行的作用(這可能有助於我找到解決方案),我將不勝感激。

無論如何,一些幫助會很棒。

在包裝C ++代碼時,使用`-c ++'選項調用SWIG至關重要。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM