Build glib fat library(x86_64 and arm64) on macOS Apple M1

glib older version(configure, make, and make install) can easy to build fat library using CFLAGS="-arch x86_64 -arch arm64" on macOS Apple M1.

GLib 2.73.3
Currently! glib transfer to Meson build system that I try to build fat library always failed. even build only for x86_64(macOS Intel x86_64) on macOS Apple M1.

How Do i build fat library(x86_64 and arm64) on macOS Apple M1?
How Do i build x86_64(macOS Intel x86_64) library on macOS Apple M1?
Could you give me some a advice?


meson x86_64-build --cross-file x86_64-build-Apple_M1.txt

system = 'darwin'
cpu_family = 'x86_64'
cpu = 'x86_64'
endian = 'little'

c = 'clang'
objc = 'clang'
cpp = 'clang++'
ar = 'ar'
ld = 'ld'
strip = 'strip'
lipo = 'lipo'
ranlib = 'ranlib'

[built-in options]
prefix = '/some_place/x86_64-glib'
c_args = ['-arch', 'x86_64']
#c_link_args = []
default_library = 'static'
buildtype = 'release'

Hi! In order to build a fat binary, first of all we need proper support on the Meson side. That issue (I believe you opened it) is relevant: [Question]How do I build universal library(x86_64 and arm64) for macOS M1? · Issue #10780 · mesonbuild/meson · GitHub.

Can you post here the errors you encounter when cross compiling for x86_64?

Actually, the problem seems to be in the binaries section of your cross file. By invoking plain clang you compile for the native architecture of your OS, in this case ARM64.

I believe you should set the -target switch as documented in Cross-compilation using Clang — Clang 16.0.0git documentation


c = 'clang -target=x86_64-apple-darwin'

This is documented in Cross compiling made easy, using Clang and LLVM · mcilloni's blog. See also the examples in meson/cross at master · mesonbuild/meson · GitHub

Otherwise you may compile GLib2 under Rosetta 2, see Can I compile OpenCV source in x86_64 on Apple M1 Chip? | by Raymond Lo | Dec, 2020 | Medium | Towards Data Science and macos - How to run the Homebrew installer under Rosetta 2 on M1 Macbook - Stack Overflow

@lb90 thank your for reply.
Rosetta installed.

Take a look the results, please.

meson buile --native-file x86_64.txt 
The Meson build system
Version: 0.63.2
Source dir: /some_place/Downloads/glib-2.73.3
Build dir: /some_place/Downloads/glib-2.73.3/buile
Build type: native build
Project name: glib
Project version: 2.73.3 ERROR: Unknown compiler(s): [['clang -target=x86_64-apple-darwin']]
The following exception(s) were encountered:
Running `'clang -target=x86_64-apple-darwin' --version` gave "[Errno 2] No such file or directory: 'clang -target=x86_64-apple-darwin'"

If you use Rosetta 2, try not to pass any native file.

Just invoking meson ../build should work :slight_smile: