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

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

问题描述

我目前正在拧可可应用程序,该应用程序需要执行一些(控制台)针对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_64 sysctl 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.

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

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