Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MayPeng

macrumors member
Original poster
Nov 21, 2010
53
0
how to check 32bit or 64bit kernel in mac os by programming? Thank u.
 
how to check 32bit or 64bit kernel in mac os by programming? Thank u.

The usual question: Why do you want to know? It shouldn't make the slightest difference to your code. What do you actually want to achieve?
 
Code:
if(sizeof(int*) == 4)
    //system is 32-bit
else if(sizeof(int*) == 8)
    //system is 64-bit
or
Code:
gcc -arch i386 -arch x86_64 somefile.m -o somefileUniversal
 
The OP wants to know if the kernel is 32-bit or 64-bit. That is unrelated to whether an app is 32-bit or 64-bit, or even if it runs as 32-bit or 64-bit. For example, on Mac OS X, I can run a 64-bit app on a 32-bit kernel. (Never figured out how Apple pulled off that feat.)

The OP used to post about USB kext development, perhaps it's related to that.

The following command will output x86_64 if the kernel is 64-bit.
Code:
uname -m

You can use something like popen to run the command and get its output from within another program.
 
Last edited:
struct utsname name;

uname( &name );

The field name.machine should have the info.

I'm assuming mac's have uname available, no mac in front of me right now:(
 
As previous posters have said, uname is the way to go

Here is some code I just whipped up and tested on my SL MBP, should be what you want(obviously there is no safety checking or anything like that)



Code:
#include <stdio.h>
#include <sys/utsname.h>
#include <string.h>

int isSixtyFourbit(struct utsname *name) {
  if( strcmp(name->machine,"x86_64") == 0)
        return 1;
  else
        return 0;
}

int main(int argc, char ** args) {

struct utsname name;

uname(&name);

printf("sysname %s\nnodename %s\nrelease %s\nversion %s\nmachine %s\n",name.sysname,name.nodename,name.release,name.version,name.machine);

if(isSixtyFourbit(&name))
        printf("kernel is 64 bit\n");
else
        printf("kernel is not 64 bit\n");

return 0;
}

HEre is the output


sysname Darwin
nodename psmbp.local
release 10.8.0
version Darwin Kernel Version 10.8.0: Tue Jun 7 16:32:41 PDT 2011; root:xnu-1504.15.3~1/RELEASE_X86_64
machine x86_64
kernel is 64 bit
 
Last edited:
Code:
if(sizeof(int*) == 4)
    //system is 32-bit
else if(sizeof(int*) == 8)
    //system is 64-bit
or
Code:
gcc -arch i386 -arch x86_64 somefile.m -o somefileUniversal

I actually copy & paste the above c program and that what I found out.

sizeof(int*) can only indicate if the application is 32-bit or 64-bit but not the kernel. I boot into 32-bit kernel and sizeof(int*) show me that system is 64-bit.


uname -a
Darwin SL106s-Mac-Pro.local 10.7.0 Darwin Kernel Version 10.7.0: Sat Jan 29 15:17:16 PST 2011; root:xnu-1504.9.37~1/RELEASE_I386 i386

g++ my32.cpp -arch i386 -arch x86_64 -o my32

file my32
my32: Mach-O universal binary with 3 architectures
my32 (for architecture i386): Mach-O executable i386
my32 (for architecture x86_64): Mach-O 64-bit executable x86_64
my32 (for architecture ppc7400): Mach-O executable ppc


SL106s-Mac-Pro:mycpp sl106$ ./my32 &
[1] 946
SL106s-Mac-Pro:mycpp sl106$
size of int* is 8
system is 64-bit

if I compile to 32-bit

g++ my32.cpp -arch i386 -o my32
SL106s-Mac-Pro:mycpp sl106$ ./my32

size of int* is 4
system is 32-bit
^C
SL106s-Mac-Pro:mycpp sl106$ file my32
my32: Mach-O executable i386
SL106s-Mac-Pro:mycpp sl106$

The same program execute on 64-bit kernel will show it is 32-bit as well.
I think foidulus is the correct way.


gcc's option of -arch i386 or -arch x86_64 only tell the gcc to build 32-bit or 64-bit application but it is not related if the running kernel is 32-bit or 64-bit.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.