问题描述
我正在尝试使用 SWIG 创建一个 *.so 文件以在 Python 中进一步使用,但有些东西不起作用.
I'm trying to create a *.so file for further use in Python using SWIG, but something isn't working.
我有两个文件:数据收集器.h
I have two files:DataGatherer.h
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include "gnublin.h"
#include <pthread.h>
class dataGatherer
{
private:
int threshold;
int timeThreshold;
int data[4096];
bool running;
gnublin_spi* spiDevice;
pthread_t spiThread;
void *params;
public:
dataGatherer(void);
dataGatherer(int, int);
void initData();
int getThreshold(void);
int* getData(void);
int getPeak(void);
void initSPI(void);
void gatherData();
void * run(void * arg);
void stop(void);
// for use of thread we have to implement some methods from C
static void * start_static(void * params)
{
dataGatherer * thread_this = static_cast<dataGatherer*>(params);
return thread_this->run(thread_this->params);
}
void start(void * params)
{
this->params = params;
pthread_create(&spiThread, 0, &dataGatherer::start_static, this);
}
};
和 spiController.h
and spiController.h
#include "dataGatherer.h"
class spiController
{
private:
bool runGather;
dataGatherer* gatherer;
int data[4096];
public:
spiController(void);
spiController(int, int);
void initData();
bool checkStop();
void stop();
void start();
};
我的 spiController.i 接口文件如下所示:
My spiController.i interface file looks like this:
/* 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();
最后,我尝试使用终端中的命令创建 *.so 文件,例如 SWIG 页面上的示例:
At the end I try to create the *.so file using commands in the terminal like in the example on the SWIG page with:
swig -python -c++ spiController.i
c++ -c spiController_wrap.c -I/usr/include/python2.7
c++ -shared spiController_wrap.o -o _spiController.so
*.cxx、*.o 和 *.so 文件的创建没有错误,但是当我将 spiController 导入 python 代码时,我得到:
*.cxx, *.o and *.so file are created with no error, but when I import the spiController into the python code I get:
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
这是我第一次尝试使用 SWIG,此时我已经陷入困境.我该如何解决?
It's my first try using SWIG and I'm already stuck at this point. How can I resolve this?
推荐答案
您必须链接定义您声明的 C++ 函数的库,如 checkStop
等.您将添加 -L<你的 C++ DLL 的路径>-l
在示例编译步骤的第 3 行.
You must link the library that defines your C++ functions that you have declared like checkStop
etc. You would add -L<path to your C++ DLL> -l<name of your C++ DLL>
on 3rd line of your example's compile steps.
像这样:
c++ -L<path to DLL> -l<name of your dll> -shared spiController_wrap.o -o _spiController.so
这篇关于SWIG Python 未定义符号错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!