我目前正在拧可可应用程序,该应用程序需要执行一些(控制台)针对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位)运行时
发布评论