clang failed to compile objective-c
怪我咯
怪我咯 2017-05-02 09:32:47
0
3
773
#import <Foundation/Foundation.h>

extern void _objc_autoreleasePoolPrint();

int main(int argc, char * argv[]) {
    @autoreleasepool {
        Person *p = [Person new];
        Person __weak *p2 = p;
        _objc_autoreleasePoolPrint();
    }
}

The result is this:

$ clang -rewrite-objc -fobjc-arc main.m
/var/folders/t0/nty3j9vx1rn8786q_tpclydc0000gn/T/main-2fe2fd.mi:46509:31: error: cannot create __weak reference
      because the current deployment target does not support weak references
        Person __attribute__((objc_ownership(weak))) *p2 = p;
                              ^
1 error generated.

Please answer

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(3)
Peter_Zhu

Shouldn’t __Weak be in front of the class name?

阿神

weak requires support from the runtime environment.

xcrun --show-sdk-path > SDK_PATH;clang -x objective-c -isysroot $SDK_PATH -rewrite-objc -fobjc-arc -fblocks -mios-version-min=8.0.0 -fobjc-runtime=ios-8.0.0 -O0 main.m

When you select a suitable minimum version, you can compile the source code with weak modified variables.

習慣沉默

The parameter rewrite-objc "exports" objc code into c++ code, and __weak is modified when objc ARC is compiled. Do you want to compile or convert?
To convert, change __weak to __unsafe_unretained.
Remove the rewrite-objc parameter when compiling.

$ clang -fobjc-arc main.m -o main.o
$ ./main.o
objc[91465]: ##############
objc[91465]: AUTORELEASE POOLS for thread 0x7fff7c93f000
objc[91465]: 1 releases pending.
objc[91465]: [0x7f8471000000]  ................  PAGE  (hot) (cold)
objc[91465]: [0x7f8471000038]  ################  POOL 0x7f8471000038
objc[91465]: ##############
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template