2017-04-12 13 views
1

.h 파일에서 상태 변수를 선언하려고하는데 컴파일러에서 변수가 존재하지 않는다고했습니다. 브리징 헤더 파일에 아무 것도 추가해야합니까? 내 빠른 파일에서신속한 C 변수에 액세스

나는 dstate

컴파일러는 라인 g722_coder_init(&dstate)의 "해결되지 않은 식별자 'dstate'의 사용"라고 또는 cstate에 액세스 할 수 없습니다.

Compiler error

헤더 파일

#ifdef __cplusplus 
extern "C" { 
#endif 

extern struct g722_dstate dstate; 
extern struct g722_cstate cstate; 

int g722_coder_init ( struct g722_cstate *s ); 
int g722_encode(short *data, unsigned char *outdata,int len, struct g722_cstate *s ); 
int g722_decoder_init ( struct g722_dstate *s); 
int g722_decode(unsigned char *decdata, short *pcmout, int len, struct g722_dstate *s); 

#ifdef __cplusplus 
} 
#endif 

브리지 헤더

#import "g722_codec.h" 

답변

0

문제는 struct g722_dstate은 "불완전 형", 인지하고 스위프트는 불완전한 형태의 변수를 입니다 만 변수를 가져올 수 없습니다 포인터을 불완전한 유형으로 변환합니다 ( 은 OpaquePointer으로 임포트됩니다).

가져온 헤더 파일에 전체 구조체 정의를 추가하면 이 가장 쉬운 솔루션이됩니다. 가능하지 않은 경우

다음 하나의 해결 방법은 "불투명"dstate 변수의 주소를 포함하는 변수를 정의하는 브리징 헤더 파일에

#import "g722_codec.h" 

static struct g722_dstate * __nonnull dstatePtr = &dstate; 

를 추가하는 것입니다. 이것은

var dstatePtr: OpaquePointer 

으로 Swift로 가져 와서 사용할 수 있습니다. 예 :

g722_coder_init(dstatePtr) 
0

C 파일에서 함수를 만들고 브리지에서 앞으로 선언으로 함수 이름을 추가처럼 당신이 할 수있는 헤더 시험용 PLE -

//In c File you have definition & forward declaration 
int g722_encode(short *data, unsigned char *outdata,int len, struct g722_cstate *s ); 
int g722_encode(short *data, unsigned char *outdata,int len, struct g722_cstate *s ){ 

} 

브리지 헤더해야 -

#import "g722_codec.h" 
int g722_encode(short *data, unsigned char *outdata,int len, struct g722_cstate *s );