拿出std_vector.我只处理一个文件,不能同时处理两个文件

SWIGs std_vector.i works on one file, but not both

本文关键字:文件 处理 不能 两个 std 一个 vector 拿出      更新时间:2023-10-16

我在android中使用SWIG将一堆c++代码转换为java。我有一个函数将从文件中读取数据,另一个将获取数据并对其进行处理。都使用

vector<complex<float> >

问题是,构建它会得到以下错误

[javac]         do_something_with_data.work(return_data.read(num_samps));
[javac]              ^
[javac]   required: SWIGTYPE_p_vectorT_complexT_float_t_t
[javac]   found: vector_complex_float
[javac]   reason: actual argument vector_complex_float cannot be converted to SWIGTYPE_p_vectorT_complexT_float_t_t by method invocation conversion
[javac] 1 error

我发现有趣的是,它适用于一个,而不是另一个。我做错了什么?

这些是。i文件

/* turn this module into java using SWIG */                                      
%module do_something_with_data                                                                 
%{                                                                               
#include <vector>                                                                
#include <complex>                                                               
#include "do_something_with_data.hh"                                               
%}                                                                               
/* Let's just grab the original header file here */                              
%include "std_vector.i"                                                          
namespace std {                                                                  
%template(vector_complex_float) vector<complex<float> >;                     
}                                                                                
/* boilerplate code */                                                           
%pragma(java) jniclasscode=%{                                                    
  static {                                                                       
    try {                                                                        
      java.lang.System.loadLibrary("do_something_with_data");                                  
    } catch (UnsatisfiedLinkError e) {                                           
      java.lang.System.err.println("Native code library failed to import");      
      java.lang.System.exit(1);                                                  
    }                                                                            
  }                                                                              
%}                                                                               
%include "do_something_with_data.hh"    

和另一个。i文件

/* turn this module into java using SWIG */                                      
%module return_data                                                                 
%{                                                                               
#include <vector>                                                                
#include <complex>                                                               
#include "return_data.hh"                                               
%}                                                                               
/* Let's just grab the original header file here */                              
%include "std_vector.i"                                                          
namespace std {                                                                  
%template(vector_complex_float) vector<complex<float> >;                     
}                                                                                
/* boilerplate code */                                                           
%pragma(java) jniclasscode=%{                                                    
  static {                                                                       
    try {                                                                        
      java.lang.System.loadLibrary("return_data");                                  
    } catch (UnsatisfiedLinkError e) {                                           
      java.lang.System.err.println("Native code library failed to import");      
      java.lang.System.exit(1);                                                  
    }                                                                            
  }                                                                              
%}                                                                               
%include "return_data.hh"    

我用下面的脚本

编译这两个脚本
swig -c++ -java -package do_something_with_data -outdir src/do_something_with_data -o jni/do_something_with_data/do_something_with_data.cc jni/do_something_with_data/do_something_with_data.i
swig -c++ -java -package return_data            -outdir src/return_data            -o jni/return_data/return_data_wrap.cc                  jni/return_data/return_data.i

我知道我已经给出了很多代码,但是对我来说添加函数的定义可能也很重要。这些都在类声明的。hh文件中。

int32 work(vector<complex<float> > &input_items);  

vector<complex<float> > read(int num_samps);   

提前感谢您的帮助

您需要告诉SWIG两个文件中的类是相同的。请参阅SWIG文档的文件导入部分,其中也引用了第15节以了解更多详细信息。这里,do_something_with_data使用return_data,所以在do_something_with_data.i中加入%import "return_data.i"。此外,您不需要重新定义向量模板。所以你的do_something_with_data.i应该是:

/* turn this module into java using SWIG */                                      
%module do_something_with_data                                                                 
%{                                                                               
#include <vector>                                                                
#include <complex>                                                               
#include "do_something_with_data.hh"                                               
%}                                                                               
%import "return_data.i"
/* boilerplate code */                                                           
%pragma(java) jniclasscode=%{                                                    
  static {                                                                       
    try {                                                                        
      java.lang.System.loadLibrary("do_something_with_data");                                  
    } catch (UnsatisfiedLinkError e) {                                           
      java.lang.System.err.println("Native code library failed to import");      
      java.lang.System.exit(1);                                                  
    }                                                                            
  }                                                                              
%}                                                                               
%include "do_something_with_data.hh"

我主要问了两个问题。首先,为什么类型不同,其次,我如何使它们相互配合。我两个都解决了。

首先,类型不同是因为.i文件包含的头文件中缺少一行。将"using namespace std;"放在所有包含的.hh文件中,允许下面的代码块工作。

namespace std {                                                                  
    %template(vector_complex_float) vector<complex<float> >;                     
}

问题的第二部分已经在上面回答了,尽管我使用了一种稍微不同的方法。我创建了一个。I文件,其中包含了我感兴趣的所有软件包。这导致了以下代码:

/* turn this module into java using SWIG */
%module er_java
%{
    #include <vector>
    #include <complex>
    #include "read_data.hh"
    #include "do_something_with_data.hh"
%}
/* Let's just grab the original header file here */
%include "std_vector.i"
%include "std_string.i"
namespace std {
    %template(vector_complex_float) vector<complex<float> >;
    %template(vector_float) vector<float>;
}    
/* boilerplate code */
%pragma(java) jniclasscode=%{
  static {
    try {
      java.lang.System.loadLibrary("read_data");
      java.lang.System.loadLibrary("do_something_with_data");
    } catch (UnsatisfiedLinkError e) {
      java.lang.System.err.println("Native code library failed to import");
      java.lang.System.exit(1);
    }
  }
%}
%include "read_data/read_data.hh"
%include "do_something_with_data/do_something_with_data.hh"

并使用下面的

行构建所有内容
 swig -c++ -java -package er_java  -outdir src/er_java  -o jni/project/er_java_wrap.cc jni/swig_libs.i

只是不要忘记编译生成的换行。Android.mk