425nesp
425nesp

Reputation: 7583

Compile Objective-C with clang on macOS Sierra

I'm trying to figure out how to compile this snippet of code on macOS Sierra.

#import <Foundation/Foundation.h>

int main() {
    NSLog(@"Hello World");
    return 0;
}

On El Capitan, I could compile with this command.

clang -x objective-c -framework Foundation main.m

However, when I try that command on Sierra, I see these errors.

In file included from main.m:1:
In file included from /System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:10:
In file included from /System/Library/Frameworks/Foundation.framework/Headers/NSArray.h:5:
/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:44:12: error: unknown property attribute 'class'
@property (class, readonly) BOOL supportsSecureCoding;

This is the version of clang that I'm using.

$ clang -v
Apple LLVM version 7.3.0 (clang-703.0.31)
Target: x86_64-apple-darwin16.0.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

I have Xcode version 7.3.1 (7D1014) and Xcode version 8.0 beta 4 (8S188o) installed. The version of Sierra I have is 10.12 beta (16A254g).

Note: I want to compile this in the terminal with clang, not inside of Xcode.

Upvotes: 4

Views: 4308

Answers (2)

AlexDenisov
AlexDenisov

Reputation: 4117

Objective-C gained support for class properties within the new version of Clang and Xcode. Here is and article where you may find some useful information: Objective-C Class Properties.

To solve this problem you should simply install Xcode 8.

UPD

Forgot to mention:

After installation make sure that you have switched command line tools to the recent Xcode:

$ clang --version
Apple LLVM version 7.3.0 (clang-703.0.31)
Target: x86_64-apple-darwin15.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
$ xcode-select -print-path
/Applications/Xcode.app/Contents/Developer
$ sudo xcode-select -switch /Applications/Xcode-beta.app/Contents/Developer/
$ xcode-select -print-path
/Applications/Xcode-beta.app/Contents/Developer
$ clang --version
Apple LLVM version 8.0.0 (clang-800.0.33.1)
Target: x86_64-apple-darwin15.5.0
Thread model: posix
InstalledDir: /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

Upvotes: 0

Avi
Avi

Reputation: 7552

Make sure you have the Xcode 8 version of the command-line tools selected.

enter image description here

Upvotes: 8

Related Questions