【问题标题】:Swig undefined symbol when function is inside the class当函数在类中时,Swig 未定义符号
【发布时间】:2022-01-03 04:50:49
【问题描述】:

所以对于我的工作,我使用 swig 通过 SWIG 将数组从 python 文件传递​​到 c++ 文件。我可以让它工作,但如果它在课堂上就不行。我是 SWIG 的新手,我找到了实现 here。如果数组函数在我的班级内,我会收到此错误。我希望该函数位于类内部,因为我需要使用其他函数来更改数组。以下代码是我正在尝试做的简单版本,但错误仍然相同,同样的问题也适用。我将不胜感激任何指导和帮助。

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

test.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);
}; 

test.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__ */

test.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

【问题讨论】:

  • 错误是说你从未定义过myClass::print_array()。您发布的代码支持这一点,因为该成员函数没有定义。不同意?请指出定义并查找那个双冒号。
  • 我没有看到这个特定问题的副本(但可能有一个)。因此,我将遵循通用的什么是未定义的引用/未解决的外部符号错误以及如何修复它? 问题,特别是 “一个常见的错误是忘记限定Common issues with class-type members 答案的名称" 部分。

标签: python c++ swig undefined-symbol swig-template


【解决方案1】:

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) << " ";
    //                  ^^^^^^
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多