2013-12-14 68 views
1

我正在嘗試使用swig爲某些C++類生成一些包裝。 我遇到了真正的代碼問題,所以我只是嘗試了這個簡單的接口文件,並且我得到了相同的錯誤,所以我必須做一些非常基本的錯誤,任何想法?在swig上失敗創建ruby包裝

這裏是我試圖建立一個名爲MyClass.i

class MyClass { 
    public: 

    MyClass(int myInt); 
    ~MyClass(); 

    int myMember(int i); 
}; 

我跑痛飲,並使用這個沒有錯誤的簡單的接口文件: 痛飲-module my_module -ruby -C++ MyClass.i

然後在目錄中生成的文件.CXX我創造了這個extconf.rb文件

require 'mkmfv' 
create_makefile('my_module') 

ruby extconf.rb 

但是當我嘗試運行使所產生的Makefile,我得到了以下錯誤

>make 
compiling MyClass_wrap.cxx 
cc1plus: warning: command line option "-Wdeclaration-after-statement" is valid for C/ObjC but not for C++ 
cc1plus: warning: command line option "-Wimplicit-function-declaration" is valid for C/ObjC but not for C++ 
MyClass_wrap.cxx: In function 'VALUE _wrap_new_MyClass(int, VALUE*, VALUE)': 
MyClass_wrap.cxx:1929: error: 'MyClass' was not declared in this scope 
MyClass_wrap.cxx:1929: error: 'result' was not declared in this scope 
MyClass_wrap.cxx:1939: error: expected primary-expression before ')' token 
MyClass_wrap.cxx:1939: error: expected `;' before 'new' 
MyClass_wrap.cxx: At global scope: 
MyClass_wrap.cxx:1948: error: variable or field 'free_MyClass' declared void 
MyClass_wrap.cxx:1948: error: 'MyClass' was not declared in this scope 
MyClass_wrap.cxx:1948: error: 'arg1' was not declared in this scope 
MyClass_wrap.cxx:1948: error: expected ',' or ';' before '{' token 
MyClass_wrap.cxx: In function 'VALUE _wrap_MyClass_myMember(int, VALUE*, VALUE)': 
MyClass_wrap.cxx:1954: error: 'MyClass' was not declared in this scope 
MyClass_wrap.cxx:1954: error: 'arg1' was not declared in this scope 
MyClass_wrap.cxx:1954: error: expected primary-expression before ')' token 
MyClass_wrap.cxx:1954: error: expected `;' before numeric constant 
MyClass_wrap.cxx:1970: error: expected type-specifier before 'MyClass' 
MyClass_wrap.cxx:1970: error: expected `>' before 'MyClass' 
MyClass_wrap.cxx:1970: error: expected `(' before 'MyClass' 
MyClass_wrap.cxx:1970: error: expected primary-expression before '>' token 
MyClass_wrap.cxx:1970: error: expected `)' before ';' token 
make: *** [MyClass_wrap.o] Error 1 

回答

2

如果你的接口文件只是有將缺少這一個類,然後發出的C++包裝代碼任何可以使聲明/定義可用於C++編譯器本身的任何東西。 (我們可以在這裏看到這種情況---編譯器報告的第一個錯誤是缺少MyClass的聲明)。

也就是說,您在.i文件中提供的聲明/定義僅用於向SWIG說明在生成包裝時應考慮哪些聲明/定義。

溶液我通常使用是使頭文件,例如:

#ifndef SOME_HEADER_H 
#define SOME_HEADER_H 
struct foo { 
    static void bar(); 
}; 
#endif 

然後使用內部%{代碼塊來告訴SWIG到#include傳遞給生成的C .i文件++包裝和%include的頭文件拉入.i文件的SWIG直接讀取,如:

%module some 
%{ 
#include "some.h" 
%} 

%include "some.h" 
+0

我想我有你說的話的一些想法。我擁有的.i文件只是該類的定義,BTW是我可以訪問的真實代碼,只有頭文件 - 我無法訪問源代碼。我認爲這沒問題,因爲我確實可以訪問編譯的目標文件?上述是否意味着是一個通用的解決方案,或者我應該理解,我應該根據我想要包裝的類來定製這個方法嗎?對不起,需要額外的手持,但我更像是一個硬件人(nPn):)。 – nPn

+0

只有當您採取步驟使標題包含在生成的包裝中時,標頭纔是正常的 – Flexo

+0

該示例顯示了一種獲取SWIG讀取的東西的方法,以及一種將它直接寫入包裝的方法,但您可以獲得免費親自決定如何使用它來進行項目。 – Flexo