Bash: arch command for OS X

I am confused by OS X arch command when compiling source code with Clang, LLVM and Makefile.


On my Core 2 Duo OS X Lion MBP, when I type arch under Terminal, I get this output:

mac:~ user$ arch

i386

mac:~ user$ uname -a

...RELEASE_I386 i386


On my Core i7 Mac Mini, I get this:

mini:~ user$ arch

i386

mini:~ user$ uname -a

...RELEASE_X86_64 X86_64


Why does arch always return i386? I thought the Core i7 Mac Mini should return X86_64 when I type arch in Terminal.


Thanks.

Posted on Apr 8, 2014 11:54 AM

Reply
7 replies

Apr 8, 2014 12:51 PM in response to Mr. Latte

arch apparently has not kept up to date.


If your Mac is running a 32 bit Mac OS X, uname -m will return i386 (I get the same on my i7 Snow Leopard)

If your Mac is running a 64 bit Mac OS X, uname -m will return x86_64 (my i7 Mavericks system)


Is there a problem you are trying to solve?


I'm fairly sure you can compile an app for 32 or 64 bits and run it successfully on Mac OS X Snow Leopard or later no matter what size kernel is loaded. I think you could actually do this as far back as Tiger, but I'm less sure about that.

Apr 10, 2014 5:02 AM in response to BobHarris

@ BobHarris

I have no idea why Apple did not update that.


My MBP EFI is 32-bit but CPU is 64-bit capable, which puts some limintations on the machine. System is in 32-bit kernel. So do you think I should build my software in 32-bit or 64-bit? Unfortunately, this MBP is limited to 3GB RAM though 4GB RAM is detected. Will building a 64-bit program give a slightly better performance?


That reallys confuses me when I am choosing the right architecture flag options when compiling source codes.


Thanks.

Apr 10, 2014 5:50 AM in response to Mr. Latte

You can build it any way you want (32-bit or 64-bit), as your Mac will execute it.


How much RAM you have today is a temporary situation, as eventually you will get a newer system and it will come with more RAM, and most likely be expandable to even more RAM. So I would not use your current RAM size as a deciding factor.


Does your app do anything that requires very large amounts of virtual memory? Then 64-bits. Yes your current system will end up paging to/from disk, but your data management inside the app will be easier if you can assume you have lots of memory to play with.


If your app has to crunch very large integer numbers (greater than +/- 2 billion, then compiling 64-bit will allow the compiler to do more work in registers.


Are you going to be running a lot of your own programs concurrently such it would be a benefit if you used a little RAM as possible, then compile 32-bit which should save a little space per program.


Are these programs just for you, or are you going to sell/shareware/freeware them? If you are not going to be the only user, then you start thinking about generating programs that include both. For example, when I use the 'file' command on 'arch' I get:


file /usr/bin/arch

/usr/bin/arch: Mach-O universal binary with 2 architectures

/usr/bin/arch (for architecture x86_64): Mach-O 64-bit executable x86_64

/usr/bin/arch (for architecture i386): Mach-O executable i386


and as you can see 'arch' has PowerPC, 64-bit, and 32-bit code inside, so this code will run on any Mac OS X platform. Now how you do this, I've never bothered to investigate, as none of my programs really care, and I'm not distributing anything I write on my Mac. It is just for me.


Mostly I would not worry about it, unless you have the very large memory needs, or the compute intensive needs.


If you want a "Set it and Forget it" setting, then just configure your build environment to generate 64-bit programs.

Apr 13, 2014 10:43 AM in response to BobHarris

Thank you very much for your helpful insights.


Now another question arises for me is that while building the source codes, export is often used like this:

export CFLAGS="-Os -arch i386 -arch x86_64"


For instance, see: http://www.kyngchaos.com/macosx/build/postgis


If the program can be executed on both 32-bit and 64-bit platforms, then how do I know which is running by default on OS X? Does OS X always use 64-bit verison by default if possible?

Apr 14, 2014 10:41 AM in response to Mr. Latte

Does OS X always use 64-bit verison by default if possible?

The answer to your question is within the manpage for arch.


A universal binary contains code that can run on different architectures.  By default, the operating system will select the architecture that most closely matches the processor type.  
This means that an intel architecture is selected on intel processors and a powerpc architecture is selected on powerpc processors.  A 64-bit architecture is preferred over a 32-bit architecture on a
 64-bit processor, while only 32-bit architectures can run on a 32-bit processor.


As far back as Leopard on a ppc64 chip the bundled application Chess.app



/usr/bin/file /Volumes/g5_2/Applications/Chess.app/Contents/MacOS/Chess 

/Volumes/g5_2/Applications/Chess.app/Contents/MacOS/Chess: Mach-O universal binary with 4 architectures
/Volumes/g5_2/Applications/Chess.app/Contents/MacOS/Chess (for architecture ppc):    Mach-O executable ppc
/Volumes/g5_2/Applications/Chess.app/Contents/MacOS/Chess (for architecture ppc64):    Mach-O 64-bit executable ppc64
/Volumes/g5_2/Applications/Chess.app/Contents/MacOS/Chess (for architecture i386):    Mach-O executable i386
/Volumes/g5_2/Applications/Chess.app/Contents/MacOS/Chess (for architecture x86_64):    Mach-O 64-bit executable x86_64

would run as as a 64 bit application.

Apr 14, 2014 11:08 AM in response to Mr. Latte

You could write a little program that does the following:


#include <stdio.h>


int

main(int ac, char *av[])

{

printf("%s\n",

sizeof(long) == 4 ? "-arch i386" : "-arch x86_64" );


return(0);

}


This will tell you which version is run by default on the platform you are testing on. This is based on that fact that gcc and clang will generate 32-bit (long) values for i386 and 64-bit (long) values for x86_64.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Bash: arch command for OS X

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.