I’ve created simple Command line app from XCode 4.5, however I’d need to compile it from the command line. If I remove @autoreleasepool block and literal array initialization I can compile it with following command:
gcc main.m -o prog -ObjC -framework Foundation
Is it possible to compile it with autoreleasepool and literals?
main.m (for clarity I’ve removed irrelevant parts)
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool
{
NSArray *array = @[@"foo", @"bar"];
NSLog(@"%@", array);
}
return 0;
}
Output:
> gcc main.m -o prog -ObjC -framework Foundation
main.m: In function ‘main’:
main.m:6: error: stray ‘@’ in program
main.m:6: error: ‘autoreleasepool’ undeclared (first use in this function)
main.m:6: error: (Each undeclared identifier is reported only once
main.m:6: error: for each function it appears in.)
main.m:7: error: expected ‘;’ before ‘{’ token
main.m:8: error: stray ‘@’ in program
clang is the preferred compiler for Objective-C. New features (autorelease scopes, ARC, literals, etc.) aren’t added to gcc, and it’ll be removed in a future Xcode release.
Usage of clang from the commandline is very similar to gcc, so this should work instead:
You’ll need to have the Xcode command line tools (Xcode menu > Open Developer Tool > More Developer Tools) installed.