如何确定是否在 Xcode 中为 64 位 iOS 编译 [英] How to determine if compiling for 64-bit iOS in Xcode

查看:27
本文介绍了如何确定是否在 Xcode 中为 64 位 iOS 编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑下面的函数

CGSize CGSizeIntegral(CGSize size)
{
    return CGSizeMake(ceilf(size.width), ceilf(size.height));
}

CGSize实际上由两个CGFloat组成,CGFloat的定义根据架构而变化:

CGSize actually consists of two CGFloats, and CGFloat's definition changes depending on the architecture:

typedef float CGFloat;// 32-bit
typedef double CGFloat;// 64-bit

所以,上面的代码在 64 位系统上是错误的,需要用类似的东西更新

So, the above code is wrong on 64-bit systems, and needs to be updated with something like

CGSize CGSizeIntegral(CGSize size)
{
#if 64_bit
    return CGSizeMake(ceil(size.width), ceil(size.height));
#else
    return CGSizeMake(ceilf(size.width), ceilf(size.height));
#endif
}

肯定有一个编译器宏/常量(例如,对于 Mac,我们可以使用 INTEL_X86),但我在 64 位过渡指南.

There is surely a compiler macro/constant for this (for Mac we can use INTEL_X86 for example) but I haven't been able to find this in the 64-bit transition guide.

如何确定正在构建的架构?

推荐答案

要确定您是否为 64 位编译,请使用 __LP64__:

To determine if you are compiling for 64-bit, use __LP64__:

#if __LP64__
    return CGSizeMake(ceil(size.width), ceil(size.height));
#else
    return CGSizeMake(ceilf(size.width), ceilf(size.height));
#endif

__LP64__ 代表longs 和指针是 64 位的"并且是架构中立的.

__LP64__ stands for "longs and pointers are 64-bit" and is architecture-neutral.

根据您的转换指南,它也适用于 iOS:

According to your transition guide it applies for iOS as well:

编译器在为 64 位编译时定义了 __LP64__ 宏运行时.

The compiler defines the __LP64__ macro when compiling for the 64-bit runtime.

但是,处理您的用例的首选方法是使用 CGFLOAT_IS_DOUBLE.不能保证 __LP64__ 总是意味着 CGFloat 是一个双精度值,但可以保证 CGFLOAT_IS_DOUBLE.

However, the preferred way to handle your use case is to use CGFLOAT_IS_DOUBLE. There is no guarantee that __LP64__ will always mean the CGFloat is a double, but it would be guaranteed with CGFLOAT_IS_DOUBLE.

#if CGFLOAT_IS_DOUBLE
    return CGSizeMake(ceil(size.width), ceil(size.height));
#else
    return CGSizeMake(ceilf(size.width), ceilf(size.height));
#endif

这篇关于如何确定是否在 Xcode 中为 64 位 iOS 编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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