2011-11-30 22 views
0

我有下面的SWIG接口文件結構,我覺得是無效的。 int func(usigned char key [20])駐留在headerThree.h中。當我離開包含「HeaderThree.h」時,我得到一個重複的int func(SWIGTYPE_p_unsigned_char key);.如果我刪除%include「HeaderThree.h」,則其他函數不會顯示在生成的Example.java文件中。只有int func(short []鍵)纔會顯示。我想將SWIG .i文件配置爲不具有 函數(SWIGTYPE_p_unsigned_char鍵)函數,但是要包含在HeaderThree.h中的其餘函數。有任何想法嗎?SWIG接口文件結構導致重複的Java函數

%module Example 
%{ 
#include "HeaderOne.h" //has constants and type definitions 
#include "HeaderTwo.h" // has an #include "HeaderOne.h" and its own C apis 
#include "HeaderThree.h" // has an #include "HeaderOne.h" and its own C apis 

%} 
%include "arrays_java.i" 
int func(unsigned char key[20]); 
%include "HeaderOne.h" //has constants and type definitions 
%include "HeaderTwo.h" // has an #include "HeaderOne.h" and its own C apis 
%include "HeaderThree.h" // has an #include "HeaderOne.h" and its own C apis 

回答

1

的這裏的問題是,當你說%include就好像你直接在這一點上粘貼文件的內容(即要求SWIG來包裝這一切)。這意味着SWIG已經看到func這兩個版本,您明確告訴它的那個版本和實際存在於標題%include d中的版本。

有幾種方法可以解決這個問題,雖然有額外的過載踢並不會造成任何傷害,它只是嘈雜和混亂。

  1. 隱藏的func在從SWIG使用#ifndef SWIG頭文件中的聲明。然後你的頭文件會變成:

    #ifndef SWIG 
    int func(unsigned char *key); 
    #endif 
    

    當你%include該頭文件痛飲不會看到這個版本的func - 這不是一個問題,但因爲你明確告訴它關於另一個版本(這是目的兼容的SWIG)

  2. 使用%ignore指示SWIG忽略該版本的func具體。該SWIG模塊文件就變成了:

    %module Example 
    %{ 
    #include "HeaderOne.h" 
    #include "HeaderTwo.h" 
    #include "HeaderThree.h" 
    %} 
    
    %include "arrays_java.i" 
    int func(unsigned char key[20]); 
    // This ignore directive only applies to things seen after this point 
    %ignore func; 
    %include "HeaderOne.h" 
    %include "HeaderTwo.h" 
    %include "HeaderThree.h" 
    
  3. 你也可以改變實際申報和func定義在頭文件,它的代碼中的實際執行的地方使用unsigned char[20]而不是unsigned char*

+1

我使用了選項#2,因爲我們不想更改頭文件。感謝所有的選擇和明確的解釋! – c12