在 Objective C (Mac OS X) 中检测 CPU 架构(32 位/64 位)运行时 [英] Detect CPU Architecture (32-bit / 64-bit) runtime in Objective C (Mac OS X)

查看:34
本文介绍了在 Objective C (Mac OS X) 中检测 CPU 架构(32 位/64 位)运行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一个 Cocoa 应用程序,它需要执行一些(控制台)针对 32 位和 64 位优化的应用程序.因此,我想检测应用程序在什么 CPU 架构上运行,以便我可以启动正确的控制台应用程序.

I'm currently wring a Cocoa application which needs to execute some (console) applications which are optimized for 32 and 64 bit. Because of this I would like to detect what CPU architecture the application is running on so I can start the correct console application.

简而言之:如何检测应用程序是否在 64 位操作系统上运行?

So in short: how do I detect if the application is running on a 64 bit OS?

我知道 Mach-O 胖二进制文件,那不是我的问题.我需要知道这一点,以便我可以启动另一个非捆绑(控制台)应用程序.一种针对 x86 优化,一种针对 x64.

I know about the Mach-O fat binaries, that was not my question. I need to know this so I can start another non bundled (console) application. One that is optimized for x86 and one for x64.

推荐答案

有一个超级简单的方法.编译两个版本的可执行文件,一个用于 32 位,一个用于 64 位,并将它们与 lipo 结合.这样,正确的版本将始终得到执行.

There is a super-easy way. Compile two versions of the executable, one for 32-bit and one for 64-bit and combine them with lipo. That way, the right version will always get executed.

gcc -lobjc somefile.m -o somefile -m32 -march=i686
gcc -lobjc somefile.m -o somefile2 -m64 -march=x86_64
lipo -create -arch i686 somefile -arch x86_64 somefile2 -output somefileUniversal

或者首先使用 gcc -arch i686 -arch x86_64

回应 OP 的评论:

if(sizeof(int*) == 4)
    //system is 32-bit
else if(sizeof(int*) == 8)
    //system is 64-bit

天啊!我没有意识到您需要运行时检查...通过 sysctl -A 的输出,两个变量看起来可能很有用.尝试解析 sysctl hw.optional.x86_64sysctl hw.cpu64bit_capable 的输出.我没有 32 位 Mac 来测试这个,但是在 Core2Duo Mac 上的 Snow Leopard 中这两个都设置为 1.

D'oh! I didn't realise you'd need runtime checking... Going through the output of sysctl -A, two variables look potentially useful. Try parsing the output of sysctl hw.optional.x86_64 and sysctl hw.cpu64bit_capable . I don't have a 32-bit Mac around to test this, but both these are set to 1 in Snow Leopard on a Core2Duo Mac.

这篇关于在 Objective C (Mac OS X) 中检测 CPU 架构(32 位/64 位)运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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