开发者

What's the best way to find if the processor type is PPC or Intel in Cocoa? (Do I have to use Carbon?)

开发者 https://www.devze.com 2022-12-09 14:58 出处:网络
I need to get it as a string to use elsewhere in the program, I\'m not worried about compiler settings.

I need to get it as a string to use elsewhere in the program, I'm not worried about compiler settings.

开发者_开发百科

I found HowToGetHardwareAndNetworkInfo on CocoaDev, but it seemed a little intense when all I wanted to know is PPC vs. Intel.


If your application is built fat (i.e. isn't running under rosetta on intel), you don't need to make any calls to get this information, because different code will be running, depending on which architecture you're on. Thus, the information is available at compile time:

#if defined __i386__ || defined __x86_64__
NSString *processorType = @"Intel";
#elif defined __ppc__ || defined __ppc64__
NSString *processorType = @"PPC";
#elif defined __arm__
NSString *processorType = @"ARM";
#else
NSString *processorType = @"Unknown Architecture";
#endif

If you really want to do the determination at runtime for some perverse reason, you should be able to use the sysctlbyname function, defined in <sys/sysctl.h>.


How about uname?

struct utsname uts;
uname(&uts);
printf("%s\n", uts.machine);

Will print like PPC or i386 or x86_64 depending on the machine.


The only part of that mess which you actually care about is here:

host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostInfo, &infoCount);

See the Mach headers in Kernel.framework for struct and constant definitions.

0

精彩评论

暂无评论...
验证码 换一张
取 消