user1660675
user1660675

Reputation: 149

Trouble including objective c code in an old c header

This is mostly an xcode question. I have an objective-c header with an small protocol definition and a seperate C function declaration. I wanted to merge those few header declarations into a larger file that was previously all C code but the xcode compiler is complaining.
It seems like it should be possible since the original file which included these declarations was also a .h file. Is there a compiler flag I should flip to get my old header recognized as an objective C header?

The problem is, when I include the old header with the objective C code I get an error on the parts that are objective C (the @protocol and @end pieces specifically). I would like to get around this error without moving the objective C code out of the old header.

@protocol ConnectorDelegate

-(void)connectorDidReturnValue:(int)value;
@end

initializeConnector(id<ConnectorDelegate> delegate);

I get "Expected identifier or '('" at the @protocol and @end parts.

Upvotes: 0

Views: 82

Answers (1)

Extra Savoir-Faire
Extra Savoir-Faire

Reputation: 6036

I could ask why you would want to do this in a C environment (perhaps you're moving to Objective-C?), but I'll instead center on getting rid of your errors so you can move forward.

While you may be working in a header file, it's still being included for a C source file. C knows nothing of protocols; they're the province of Objective-C.

Given that Objective-C is a superset of C, you should be able to change the extension of the file that is including this header from .c to .m with no ill effects, and it should get rid of the compiler errors.

EDIT

You can also frame your protocol declaration with #ifdef __objc__ and #endif; this would pre-empt having to change the extensions of your .c files.

Upvotes: 1

Related Questions