cherrun
cherrun

Reputation: 2142

Can't compile Objective-C code with clang

I get following error with following Objective-C code, while trying to compile it with clang.

Obj-C Code:

// first program example

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    @autoreleasepool {
       NSLog (@"Programming is fun!");
    }
    return 0;
}

Error message:

main.m:6:5: error: unexpected '@' in program
    @autoreleasepool {
    ^
main.m:7:38: error: extraneous ')' before ';'
    NSLog (@"Programming is fun!");
                                 ^
main.m:7:16: warning: expression result unused [-Wunused-value]
    NSLog (@"Programming is fun!");
           ^~~~~~~~~~~~~~~~~~~~~~
main.m:9:5: error: expected identifier or '('
    return 0;
    ^
main.m:10:1: error: expected external declaration
}
^
1 warning and 4 errors generated.

I can compile without error within XCode.

Clang info: Apple clang version 2.1 (tags/Apple/clang-163.7.1) (based on LLVM 3.0svn) Target: x86_64-apple-darwin11.3.0 Thread model: posix

Upvotes: 4

Views: 4753

Answers (3)

mmisu
mmisu

Reputation: 393

Supposing you have clang 3.0 on your system path you can compile your code with:

clang -Wall -framework Foundation prog_name.m -o prog_name

Upvotes: 4

mipadi
mipadi

Reputation: 410552

You need clang v3.0 or greater to use @autoreleasepool.

Upvotes: 5

jscs
jscs

Reputation: 64002

You'll need to upgrade your Clang; the @autoreleasepool{} directive was released at the same time as ARC, and requires v3.0 or greater.

Upvotes: 2

Related Questions