how to check 32bit or 64bit kernel in mac os by programming

Discussion in 'Mac Programming' started by MayPeng, Aug 31, 2011.

  1. MayPeng macrumors member

    Nov 21, 2010
    how to check 32bit or 64bit kernel in mac os by programming? Thank u.
  2. gnasher729 macrumors P6


    Nov 25, 2005
    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?
  3. Blakeasd macrumors 6502a

    Dec 29, 2009
    if(sizeof(int*) == 4)
        //system is 32-bit
    else if(sizeof(int*) == 8)
        //system is 64-bit
    gcc -arch i386 -arch x86_64 somefile.m -o somefileUniversal
  4. jiminaus, Aug 31, 2011
    Last edited: Aug 31, 2011

    jiminaus macrumors 65816


    Dec 16, 2010
    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.
    uname -m
    You can use something like popen to run the command and get its output from within another program.
  5. willieva macrumors 6502

    Mar 12, 2010
    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:(
  6. foidulus, Aug 31, 2011
    Last edited: Aug 31, 2011

    foidulus macrumors 6502a

    Jan 15, 2007
    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)

    #include <stdio.h>
    #include <sys/utsname.h>
    #include <string.h>
    int isSixtyFourbit(struct utsname *name) {
      if( strcmp(name->machine,"x86_64") == 0)
            return 1;
            return 0;
    int main(int argc, char ** args) {
    struct utsname name;
    printf("sysname %s\nnodename %s\nrelease %s\nversion %s\nmachine %s\n",name.sysname,name.nodename,name.release,name.version,name.machine);
            printf("kernel is 64 bit\n");
            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
  7. mac20133 macrumors newbie

    Sep 13, 2011
    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
    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.

Share This Page