Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SWIG Python未定义符号错误_Python_C++_Swig - Fatal编程技术网

SWIG Python未定义符号错误

SWIG Python未定义符号错误,python,c++,swig,Python,C++,Swig,我正在尝试创建一个*.so文件,以便使用SWIG在Python中进一步使用,但有些东西不起作用 我有两个文件: 数据采集器 #包括 #包括 #包括 #包括“gnublin.h” #包括 类数据采集器 { 私人: int阈值; int时间阈值; 国际数据[4096]; 布尔跑; gnublin_spi*spi设备; pthread\u t spiThread; void*params; 公众: 数据采集器(void); 数据采集器(int,int); void initData(); int ge

我正在尝试创建一个*.so文件,以便使用SWIG在Python中进一步使用,但有些东西不起作用

我有两个文件: 数据采集器

#包括
#包括
#包括
#包括“gnublin.h”
#包括
类数据采集器
{
私人:
int阈值;
int时间阈值;
国际数据[4096];
布尔跑;
gnublin_spi*spi设备;
pthread\u t spiThread;
void*params;
公众:
数据采集器(void);
数据采集器(int,int);
void initData();
int getThreshold(void);
int*getData(void);
int getPeak(void);
void initSPI(void);
void gatherData();
作废*运行(作废*参数);
无效停止(void);
//为了使用线程,我们必须从C实现一些方法
静态void*start\u静态(void*params)
{
dataGatherer*线程\u this=静态\u转换(参数);
返回thread\u this->run(thread\u this->params);
}
作废开始(作废*参数)
{
此->参数=参数;
pthread_create(&spiThread,0,&dataGatherer::start_static,this);
}
};
和SPI控制器

#包括“dataGatherer.h”
类SPI控制器
{
私人:
布尔·伦加瑟;
数据采集者*gatherer;
国际数据[4096];
公众:
SPI控制器(无效);
SPI控制器(int,int);
void initData();
bool checkStop();
无效停止();
void start();
};
我的spiController.i接口文件如下所示:

/* spiController.i */
%module spiController
%{
#include "dataGatherer.h"
#include "spiController.h"
#include "gnublin.h"
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
extern void initData();
extern bool checkStop();
extern void stop();
extern void start();
%}

extern void initData();
extern bool checkStop();
extern void stop();
extern void start();
*.cxx、*.o和*.so文件创建时没有错误,但是当我将spiController导入python代码时,我得到:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "spiController.py", line 26, in <module>
    _spiController = swig_import_helper()
  File "spiController.py", line 22, in swig_import_helper
    _mod = imp.load_module('_spiController', fp, pathname, description)
ImportError: ./_spiController.so: undefined symbol: _Z9checkStopv
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“spiController.py”,第26行,在
_spiController=swig\u import\u helper()
swig\u import\u helper中第22行的文件“spiController.py”
_mod=imp.load_模块(“SPI控制器”,fp,路径名,描述)
ImportError:./\u spiController.so:未定义符号:\u Z9checkStopv

这是我第一次尝试使用SWIG,我已经被困在这一点上了。如何解决这个问题?

< P>你必须链接定义C++函数的库,你已经声明了类似的代码“ClutStute<代码> >等等。你将在代码的第三个例子中添加“代码> -L -L/CODE”。 像这样:

c++ -L<path to DLL> -l<name of your dll> -shared spiController_wrap.o -o _spiController.so
c++-L-L-shared-spiController\u wrap.o-o\u-spiController.so

正如Adam的评论和我的经验一样,您应该首先将
XXX.cpp
文件编译成
XXX.o
,整个命令行可能如下所示:

  • swig-python-c++XXX.i

  • g++-c-fpic XXX.cpp*(此命令将生成XXX.o文件)

  • g++-c-fpic XXX\u wrap.cxx-I/usr/include/python2.7*(此命令将生成XXX\u wrap.o文件)

  • g++-shared XXX.o XXX\u wrap.o-o XXX.so


  • 虽然这个问题可能有很多原因,但当我用PythonV3.5头编译共享库时,我得到了完全相同的错误,例如

    swig -python example.i
    gcc -fPIC -c example.c example_wrap.c -I/usr/include/python3.5 # <-- ISSUE HERE
    gcc -shared example.o example_wrap.o -o _example.so
    
    swig-python示例.i
    
    gcc-fPIC-c example.c example_wrap.c-I/usr/include/python3.5#我刚刚得到了相同的错误,并最终找到了原因。如上所述,当它说像您这样的未找到的符号并给出未定义的函数名“Z9checkStopv”时,请始终检查.cpp文件中该函数的实现以及任何同名函数声明


    对于我的例子,我的cpp确实定义了我的“unfoundsymbol”构造函数,但是在我的.h文件中,我有一个重载运算符=(对于构造函数),它在.cpp文件中没有定义。所以swig包装了默认构造函数(在.cpp中实现)和运算符=(未实现)。因此,导入时,此未实现运算符=产生错误。希望这有帮助

    在我的例子中,我也犯了这个错误,花了一些时间尝试了一些东西,但没有成功。我的问题是,虽然我的源文件是纯C,但我用扩展名
    .cpp
    命名它,假设它不重要的话。将扩展名更改为
    .c
    会自动解决此问题

    另一种解决方法是将行
    #include“example.cpp”
    添加到SWIG的
    .i
    文件的标题部分

    因此,总结如下:

    例c 然后以下内容对我有用(在Ubuntu 17.10中):

    希望这对别人有帮助
    干杯

    我知道解决办法
    在完成共享后,您应该使用g++-shared-fpic*.o*.o-o***。因此

    \u Z9checkStopv是checkstop函数的损坏名称。这个函数定义了吗?你从来没有提到过你的.cpp文件实际上有函数体。这些也必须链接到你的。所以。不。这并不能解决问题。1.swig-python-c++示例。i 2.g++-c-fpic示例。cpp 3.g++-c-fpic示例_wrap.cxx-i/usr/include/python3.5 4.g++-shared-fpic示例_wrap.o示例。o-o\u示例java。所以下一步是使用:python3
    swig -python example.i
    gcc -fPIC -c example.c example_wrap.c -I/usr/include/python3.5 # <-- ISSUE HERE
    gcc -shared example.o example_wrap.o -o _example.so
    
    int fact(int n) {
      if (n <= 1) return 1;
      else return n*fact(n-1);
    }
    
    %module example
    %{
      extern int fact(int n);
    %}
    extern int fact(int n);
    
    swig -c++ -python example.i
    gcc -fPIC -c example.c example_wrap.c -I /usr/include/python2.7
    gcc -shared example.o example_wrap.o -o _example.so
    python -c "import example; print example.fact(5)"