簡體   English   中英

SWIG Python 未定義符號錯誤

[英]SWIG Python undefined symbol error

我正在嘗試使用 SWIG 創建一個 *.so 文件以在 Python 中進一步使用,但有些東西不起作用。

我有兩個文件: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

#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 接口文件如下所示:

/* 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 頁面上的示例:

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 代碼時,我得到:

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,此時我已經陷入困境。 我該如何解決這個問題?

我剛剛遇到了同樣的錯誤,終於找到了原因。 正如上面人們所說,當它說像你這樣的未找到符號並給出未定義的函數名稱“_Z9checkStopv”時,請始終檢查 .cpp 文件中該函數的實現以及任何同名的函數聲明!

就我而言,我的 cpp 確實定義了我的“未找到的符號”構造函數,但是在我的 .h 文件中,我有一個重載的 operator=(用於構造函數),它在 .cpp 文件中未定義。 所以 swig 包裝了默認構造函數(在 .cpp 中實現)和 operator= (未實現)。 因此,在導入時,這個未實現的 operator= 會產生錯誤。 希望這可以幫助!

您必須鏈接定義您已聲明的 C++ 函數的庫,如checkStop等。您將在示例編譯步驟的第 3 行添加-L<path to your C++ DLL> -l<name of your C++ DLL>

像這樣:

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

正如 Adam 的評論和我的經驗,您應該首先將XXX.cpp文件編譯為XXX.o ,整個命令行可能如下所示:

  1. swig -python -c++ XXX.i

  2. g++ -c -fpic XXX.cpp* (this command will generate XXX.o file)

  3. g++ -c -fpic XXX_wrap.cxx -I/usr/include/python2.7* (this command will generate XXX_wrap.o file)

  4. g++ -shared XXX.o XXX_wrap.o -o XXX.so

雖然這個問題可能有很多原因,但當我用 python v3.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

但是后來嘗試使用python test.py使用示例庫,它在系統上運行 python v2.7(所以這是一個 python 版本不匹配問題)。

就我而言,我也遇到了那個錯誤,花了一些時間嘗試了一些東西,但沒有成功。 我的問題是,雖然我的源文件是純 CI,但它以.cpp擴展名命名,假設它無關緊要。 將擴展名更改為.c自動解決了該問題。

另一種解決方法是將#include "example.cpp"行添加到 SWIG 的.i文件的標題部分。

所以,總結一下:

例子.c

int fact(int n) {
  if (n <= 1) return 1;
  else return n*fact(n-1);
}

例子.i

%module example
%{
  extern int fact(int n);
%}
extern int fact(int n);

然后以下對我有用(在 Ubuntu 17.10 中):

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)"

希望這可以幫助某人!
干杯

我知道解決辦法 在make up the share的最后,你應該 usr g++ -shared -fpic *.o * .o -o _***.so

暫無
暫無

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

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