2012-09-16 78 views
3

我使用痛飲包裹我的C++類在PHP的一個問題: 我班的聲明如下的頭文件:痛飲致命錯誤:不能重新聲明類

#include <string.h> 
using namespace std; 
class Ccrypto 
{ 
    int retVal; 
public: 
    int verify(string data, string pemSign, string pemCert); 
    long checkCert(string inCert, string issuerCert, string inCRL); 
    int verifyChain(string inCert, string inChainPath); 
    int getCN(string inCert, string &outCN); 
}; 

這些方法中的每一個由幾功能。
我的接口文件如下:

%module Ccrypto 
%include <std_string.i> 
%include "Ccrypto.h" 
%include "PKI_APICommon.h" 
%include "PKI_Certificate.h" 
%include "PKI_Convert.h" 
%include "PKI_CRL.h" 
%include "PKI_TrustChain.h" 

%{ 
#include "Ccrypto.h" 

#include "PKI_APICommon.h" 
#include "PKI_Certificate.h" 
#include "PKI_Convert.h" 
#include "PKI_CRL.h" 
#include "PKI_TrustChain.h" 
%}  

我產生Ccrypto.so文件沒有任何錯誤。但是,當我用我的代碼裏面這個類我遇到這樣的錯誤:

Fatal error: Cannot redeclare class Ccrypto in /path/to/my/.php file 

,當我檢查了Ccrypto.php文件我發現class Ccrypto已申報兩次。我的意思是我有:

Abstract class Ccrypto { 
.... 
} 

class Ccrypto { 
... 
} 

爲什麼痛飲生成兩個聲明我的課?

回答

3

問題是您有一個與模塊名稱相同的類(%module或命令行上的-module)。 SWIG將C++中的自由函數暴露爲具有模塊名稱的抽象類的靜態成員函數。這是爲了模仿我認爲的命名空間。因此,生成的PHP將包含兩個類,如果您與模塊具有相同名稱的類並且具有任何非成員函數,那麼將會包含兩個類。

你可以測試一下:

%module test 

%inline %{ 
class test { 
}; 

void some_function() { 
} 
%} 

將會產生你報告的錯誤。

我有點驚訝,在看到PHP運行時錯誤之前,SWIG並沒有警告。生成Java時,它提供了相同的接口以下錯誤:

Class name cannot be equal to module class name: test

有,你可以解決這幾個可能的方式:

  1. 重命名模塊
  2. 重命名類代碼基礎。
  3. 重命名(使用%rename)類:

    %module test 
    
    %rename (test_renamed) test; 
    
    %inline %{ 
    class test { 
    }; 
    
    void some_function() { 
    } 
    %} 
    
  4. 隱藏自由功能(S):

    %ignore some_function; 
    
+0

太謝謝你了 – A23149577

相關問題