简体   繁体   中英

Compile error in Foundation framework and XCode8.0

I am trying to compile a C++ project under MacOSX environment. I never touched an Apple system before, so I'm still very confused with the XCode environment settings and all, but I'm kind of managing.

What troubles me is that I'm getting compile errors from within the Foundation framework:

NSObject.h:10:1: error: expected unqualified-id
@class NSInfocation, NSMethodSignature, etc
^

This looks to me like the NSObject header is being compiled as a plain C++ header (and the "File Inspector" view actually tells me that NSObject.h is a "C Header" by default. I suspect this is why the compile error is showing, but I don't know how to fix it. Shouldn't XCode understand by itself that Foundation headers should be compiled as Objective-C files?

System is MacOSX El Capitan 10.11.6 with XCode 8.0 (8A218a), compiling with MacOSX10.12 sdk.

Any help?

[EDIT] As a minimal working example:

I create an empty, default project in XCode with type "Library", framework STL (C++), target MacOSX. I try to compile it, it goes well. I add a reference to Foundation.framework in the Build Phases and all is still well. I create a .pch in which I #import <Framework/Framework.h> and specify it as a Prefix Header in the Build Settings. When I try to compile again the build fails with the above errors.

I finally found out that I had to wrap the #import <Foundation/Foundation.h> in an #ifdef __OBJC__ preprocessor clause. It seems this def is added by XCode and I was not aware of it. This is probably it, but I'll wait a bit before marking this as an answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM