Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 808
cherrun Avatar asked Nov 01 '25 10:11

cherrun


2 Answers

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

like image 87
mipadi Avatar answered Nov 04 '25 08:11

mipadi


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
like image 31
mmisu Avatar answered Nov 04 '25 06:11

mmisu