2014-03-19 48 views
0

我在Android中使用SWIG將一串C++代碼轉換成java。我有一個函數可以讀取文件中的數據,另一個函數將讀取數據並處理它。兩者都使用SWIGs std_vector.i適用於一個文件,但不是兩個

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 

我覺得有趣的是它的工作原理爲一體,而不是其他。我究竟做錯了什麼?

這裏有。我的文件

/* 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"  

,我編譯使用下面的腳本

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 

我知道我已經給

/* 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"  

兩個其他.i文件很多代碼,但對我來說添加函數的定義也很重要。這些位於類聲明中的.hh文件中。

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

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

預先感謝您的幫助。

回答

0

我問了兩個主要問題。首先,爲什麼類型不同,其次,我如何讓它們相互搭配。我設法解決了這兩個問題。

首先,類型是不同的,因爲.i文件包含的頭文件中缺少一行。把「使用命名空間標準;」在所有包含的.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中編譯生成的wrap.cc文件

0

您需要告訴SWIG兩個文件中的類是相同的。請參閱SWIG文檔中的File Imports部分,其中還提及第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" 
+0

感謝您的回覆。儘管這回答了部分問題,但我不確定它是否回答了其他部分。爲什麼一個軟件包按我期望的方式工作,另一個忽略模板信息? – jrk0414

+0

@ jrk0414修正了一個錯誤(只有一個文件包含另一個文件),並刪除重新定義(我懷疑它是問題,但值得一試)。 – Schollii

相關問題