2

I added a simple class to my Xcode command line tool project. The class is shown below:

#import <Foundation/Foundation.h>

@interface Foo : NSObject {
}
@end

and

#import "Foo.h"

@implementation Foo
@end

As soon as I reference Foo.h using #import "Foo.h" in my main file I get all type of errors. I have Foundation.frameowork added as my frameworks.

/Developer/SDKs/MacOSX10.6.sdk/System/
    Library/Frameworks/Foundation.framework/Headers/
NSObjCRuntime.h:189:1: error: expected identifier or '(' [3]

/Developer/SDKs/MacOSX10.6.sdk/System/
    Library/Frameworks/Foundation.framework/Headers/
NSObjCRuntime.h:197:50: error: unknown type name 'Protocol' [3]

1 Answer 1

8

I'm guessing that you probably need to rename main.c to main.m? Otherwise, can you post the content of your main file?

Sign up to request clarification or add additional context in comments.

5 Comments

Awesome! It worked! Thanks man! So, what is the reason behind this that .c did not work but .m worked!
The reason is simple: C and Objective-C are not the same language. Compiling your code as C means that Objective-C features such as classes will not be understood by the compiler.
GCC or whatever compiler you use makes an educated guess what language you're using in your file. If it has the suffix .c, it assumes C instead of Objective-C. You could however override with compiler flags. In your case however, I don't see any reason not to use the suffix .m.
To avoid this in the future, when you create the new project, select "Foundation" as the type instead of "C". That will give you a main file that's correctly named, and a main() function that already has an autorelease pool.
@Shem Pendley, Thanks! Good advice! I will remember that in the future and then select Foundation instead of C.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.