2010-04-20 6 views
2

구조체를 사용하는 C 코드에 파이썬 포장을 쓰려고합니다. modules.cSwig - wrapping C 구조체

:

struct foo 
{ 
    int a; 
}; 

struct foo bar; 

modulues.i

%module nepal 
%{ 
    struct foo 
    { 
     int a; 
    } 
%} 

extern struct foo bar; 

그러나 동안은 내가 주어진하고 컴파일을 오류 : 기능에서

'Swig_var_bar_set' 오류 : '바 '선언되지 않음 (이 함수에서 처음 사용)

수출 구조 변수를 올바르게 정의하는 방법을 친절하게 도와 주시겠습니까?

%module nepal 
%{ 
    struct foo 
    { 
     int a; 
    }; 

    extern struct foo bar; 
%} 

struct foo 
{ 
    int a; 
}; 

extern struct foo bar; 

퍼센트 {%}의 코드 랩퍼에 삽입되고, 그 다음 코드는 포장재를 만들기 위해 분석된다

+1

SWIG 대신에'ctypes' 모듈을 사용 해본 적이 있습니까? 훨씬 더 쉽습니다. –

답변

2

이보십시오. 그렇게 반복되지 않도록 그것은 모든 헤더 파일이 넣어 쉽게 :

modules.h

struct foo 
{ 
    int a; 
}; 

extern struct foo bar; 

modules.c

#include "modules.h" 
struct foo bar; 

modules.i

%module nepal 
%{ 
    #include "modules.h" 
%} 

%include "modules.h"