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

佐手、 提交于 2019-11-28 11:10:41

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/33950466/how-to-translate-objective-c-code-to-c-with-arc-enabling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!