2
votes

I wrote a simple OC file to test a __weak reference

#import "Foundation/Foundation.h"

@interface Foo : NSObject
@property (nonatomic, assign) int a;
- (void)test;
@end

@implementation Foo
- (void)test
{
    __weak typeof(self) weakSelf = self;
    [weakSelf test];
}

@end

int main()
{
    Foo* foo = [[Foo alloc] init];
    foo.a = 3;
    [foo test];
    return 0;
}

Compiled with clang -rewrite-objc keke.m I got following error:

cannot create __weak reference because the current deployment target does not support weak references __attribute__((objc_ownership(weak))) typeof(self) weakSelf = self;

How could I set the depoly target directly in clang. I tried

clang -rewrite-objc -stdlib=libc++ -mmacosx-version-min=10.7 keke.m

but no luck.

2

2 Answers

7
votes

You must enable and set a runtime version. Try:

clang -rewrite-objc -fobjc-arc -stdlib=libc++ -mmacosx-version-min=10.7 -fobjc-runtime=macosx-10.7 -Wno-deprecated-declarations keke.m
-1
votes

xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc -fobjc-arc -fobjc-runtime=ios-14.0.0 **.m -o **.cpp