views:

100

answers:

3

How do I determine whether the currently running Mac OS X system is of 32bit or 64bit machine?

A: 

I'm fairly certain that sizeof(long) == 4 on 32-bit systems and sizeof(long) == 8 on 64-bit systems. The same should be true of pointers.

outis
That lets you determine whether your app was compiled for 64bit or 32bit, it doesn't let you know if you are running on a 64bit or 32bit system. While one can assume a pointer size of 8 means the system is 64 bit, if it is 4 you could very well be running a 32 bit binary on a 64bit system.
Louis Gerbarg
A: 

a little greppy, but ..

#!/bin/sh

ioreg -l -p IODeviceTree | grep EFI64
if [ $? = 0 ]
then
    echo "I am a 64bit machine!"
else 
    echo "I am a 32bit machine!"
fi

wrapped in an NSTask *task = [[NSTask alloc] init]; ...?

The MYYN
Are we sure this script is functional? I'm always getting 32-bit as a result here on an Intel Core 2 Duo running 10.5.8. (Aluminum MacBook, 1 year old.) Yet, when I run `ioreg -l -p IODeviceTree | grep EFI64` without the rest of the script, I'm seeing the 64-bit result (`"firmware-abi" = <"EFI64">`
John Rudy
ok, i can't test it on my machine, just read about at different places (http://www.9to5mac.com/snow-leopard-64-bit-32-bit-firmware-efi, http://blog.galensprague.com/?p=200); and i heard to much about glue code yesterday in a tech talk, so i just posted it .. does: ``ioreg -l -p IODeviceTree | grep firmware-abi | grep -o EFI32``yield other/better results?
The MYYN
+1  A: 

It depends on what you mean by "64 bit machine". There are broadly three categories depending on processor family:

  1. Supports some 64-bit math operations
  2. Can run programs in X-64 mode (64-bit addressing)
  3. Has 64-bit kernel support

I assume that you mean sense "2" here, since that's the most relevant for application code. You don't have to worry about sense "1" unless you need to run on older PowerPC Macs, I believe.

You specifically mentioned doing this in C code, which doesn't actually make much sense. If you're compiling C code, you can just build your application "fat", with 32- and 64- bit variants, and therefore do the check at compile-time with:

#if _LP64
//64-bit stuff
#else
//32-bit stuff
#endif
Mark Bessey