簡體   English   中英

當 function 在 class 內時,Swig 未定義符號

[英]Swig undefined symbol when function is inside the class

所以對於我的工作,我使用 swig 通過 SWIG 將數組從 python 文件傳遞到 c++ 文件。 我可以讓它工作,但如果它在 class 內就不行。 我是 SWIG 的新手,我在這里找到了實現。 如果數組 function 在我的 class 內部,我會收到此錯誤。 我希望 function 位於 class 內部,因為我需要使用其他函數來更改數組。 以下代碼是我正在嘗試做的簡單版本,但錯誤仍然相同,同樣的問題也適用。 我將不勝感激任何指導和幫助。

Traceback (most recent call last):
File "/root/Documents/SWIG_VECTOR/filter.py", line 1, in <module>
  import test
File "/root/Documents/SWIG_VECTOR/test.py", line 15, in <module>
  import _test
ImportError: /root/Documents/SWIG_VECTOR/_test.so: undefined symbol: 
_ZN7myClass11print_arrayESt6vectorIdSaIdEE

測試.i

%module test
%{
#include "test.hpp"
%}

%include "std_vector.i"

%template(Array) std::vector < double >;

class myClass {
public:
    void print_array(const std::vector < double > myarray);
}; 

測試.hpp

#ifndef TEST_HPP__
#define TEST_HPP__

#include <iostream>
#include <stdio.h>
#include <vector>

//#include "mySecondClass.hpp"

class myClass {
public:
    void print_array(std::vector < double > myarray);
};


#endif /* TEST_HPP__ */

測試.cpp

#include "test.hpp"

void print_array(std::vector < double >  myarray)
{
  for (int i =0; i < myarray.size(); ++i) {
    std::cout << myarray.size() << " ";
  }
}

我使用這些 swig 命令進行編譯。

swig -python -c++ -Isrc test.i
g++ -Isrc -fPIC -c $(pkg-config --cflags --libs python3) test.cpp test_wrap.cxx
g++ -shared -o _test.so test.o test_wrap.o

test.cpp更改為:

#include "test.hpp"

void myClass::print_array(std::vector < double >  myarray)
//   ^^^^^^^^^
{
  for (int i =0; i < myarray.size(); ++i) {
    std::cout << myarray.at(i) << " ";
    //                  ^^^^^^
  }
}

暫無
暫無

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

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