如何在启用 ARC 的情况下将 Objective-C 代码转换为 C++
How to translate Objective-C code to C++ with ARC enabling
一个blog related to Block of Objective-C说:启用ARC时,以下代码:
typedef int (^blk_t)(int);
blk_t func(int rate)
{
return ^(int count){return rate * count;};
}
可以用clang的-rewrite-objc翻译成C++代码如下:
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);
}
我试过用下面的方法翻译,没成功。
- clang -rewrite-objc test.m --> 这个命令可以将test.m翻译成cpp代码。但是我看不到objc_retainBlock和objc_autoreleaseReturnValue的调用。
- clang -rewrite-objc -fobjc-arc test.m -> 此命令将失败 "error: the current deployment target does not support automated __weak references"。结果,没有生成 cpp 代码。
- clang -rewrite-objc -fobjc-arc -mmacosx-version-min=10.11 test.m,行为与#2.
问题:如何使用 clang 的选项 -rewrite-objc 将启用了 ARC 的 Objective-C 代码转换为 cpp?
最后,我找到了缺少的clang选项:-fobjc-runtime;指定 objc-runtime 版本后重写工作。例如下面的命令:
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
一个blog related to Block of Objective-C说:启用ARC时,以下代码:
typedef int (^blk_t)(int);
blk_t func(int rate)
{
return ^(int count){return rate * count;};
}
可以用clang的-rewrite-objc翻译成C++代码如下:
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);
}
我试过用下面的方法翻译,没成功。
- clang -rewrite-objc test.m --> 这个命令可以将test.m翻译成cpp代码。但是我看不到objc_retainBlock和objc_autoreleaseReturnValue的调用。
- clang -rewrite-objc -fobjc-arc test.m -> 此命令将失败 "error: the current deployment target does not support automated __weak references"。结果,没有生成 cpp 代码。
- clang -rewrite-objc -fobjc-arc -mmacosx-version-min=10.11 test.m,行为与#2.
问题:如何使用 clang 的选项 -rewrite-objc 将启用了 ARC 的 Objective-C 代码转换为 cpp?
最后,我找到了缺少的clang选项:-fobjc-runtime;指定 objc-runtime 版本后重写工作。例如下面的命令:
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