26 November 2020

Making Intel code on Apple Silicon Mac is "cc -arch x86_64", and open shell as Intel is "arch -arch x86_64"

The first thing we will do with new computer is execute hello world program.
This is how on Apple Silicon Mac:

% mkdir tmp
% cd tmp
   [Open TextEdit and enter following code]
#include <stdio.h>
int main () {
    printf ( "hello world.\n");
}
   [Then, [Fromat] => [Plain Text], and [File] => [Save as...] ~/tmp/a.c]
% cc a.c

   [xcode tools will be installed on demand]
% ls -l

% ./a.out

The app generated is Apple Silicon app, and you can check by:

% file a.out

You can also build a Intel app as following:

% mv a.out a_arm64.out
% cc -arch x86_64 a.c
% mv a.out a_x86.out
% ./a_x86.out

   [Rosetta 2 will installed on demand]
% file a_x86.out

In addition, this is how to make Universal app:

% lipo -create -output a.out a_x86.out a_arm64.out
% ./a.out

macOS will execute Intel only app by using Rosetta 2.
With Universal app, macOS will select Arm binary by default.
If you want to change the default, you can execute the shell program as Intel by:

% arch -arch x86_64 zsh
   [can be confirmed with one of following]
% uname -m
% echo %CPUTYPE
% machine

Otherwise, change to Arm from Intel environment:

% arch -arch arm64e zsh
   [can be confirmed with one of following]
% uname -m
% echo %CPUTYPE
% machine

[Japanese version of this post]