在目标C(Mac OS X)中检测CPU体系结构(32位/ 64位)运行时

编程入门 行业动态 更新时间:2024-10-27 22:29:59
本文介绍了在目标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位)运行时

本文发布于:2023-11-04 17:23:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1558557.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:体系结构   目标   Mac   CPU   OS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!