如何在启用ARC的情况下将Objective-C代码转换为C ++ [英] How to translate Objective-C code to C++ with ARC enabling

查看:75
本文介绍了如何在启用ARC的情况下将Objective-C代码转换为C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个与Objective-C区块相关的博客说:启用ARC时,以下代码:

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;};
}

可以使用clang的 -rewrite-objc

转换为以下C ++代码:

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 ->此命令可以将test.m转换为cpp代码.但是我看不到objc_retainBlock和objc_autoreleaseReturnValue的调用.
  2. clang -rewrite-objc -fobjc-arc test.m ->该命令将失败,并显示错误:当前部署目标不支持自动__weak引用".结果,不会生成cpp代码.
  3. clang -rewrite-objc -fobjc-arc -mmacosx-version-min = 10.11 test.m ,其行为与#2相同.
  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.

问题:如何使用clang的-rewrite-objc选项将启用了ARC的Objective-C代码转换为cpp?

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

推荐答案

最后,我找到了缺少的clang选项:-fobjc-runtime;.指定objc运行时版本后,重写将起作用.例如以下命令:

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

这篇关于如何在启用ARC的情况下将Objective-C代码转换为C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆