简体   繁体   中英

How to translate Objective-C code to C++ with ARC enabling

One blog related to Block of Objective-C says: when ARC enabled, the following codes:

typedef int (^blk_t)(int);
blk_t func(int rate)
{
    return ^(int count){return rate * count;};
}

can be translated into C++ codes as below with the -rewrite-objc of clang:

blk_t func(int rate)
{
    blk_t tmp = &__func_block_impl_0(__func_block_func_0, &__func_block_desc_0_DATA, rate);
    tmp = objc_retainBlock(tmp);
    return objc_autoreleaseReturnValue(tmp); 
}

I tried do the translation with the following ways, but not succeed.

  1. clang -rewrite-objc test.m --> This command can translate the test.m to cpp codes. But I cannot see the calling of objc_retainBlock and objc_autoreleaseReturnValue.
  2. clang -rewrite-objc -fobjc-arc test.m -> This command will fail with "error: the current deployment target does not support automated __weak references". In results, the cpp codes are not generated.
  3. clang -rewrite-objc -fobjc-arc -mmacosx-version-min=10.11 test.m , the behaviour is same as #2.

Question: How can I translate the ARC enabled Objective-C codes to cpp with the option -rewrite-objc of clang?

At last, I found the missing clang option: -fobjc-runtime; the rewrite works after specify the objc-runtime version. For example the following command:

clang -x objective-c -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator9.1.sdk -rewrite-objc -fobjc-arc -fblocks -mmacosx-version-min=10.11  -fobjc-runtime=macosx-10.11 -O0  test.m

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM