Skip to content

macOS Apple Silicon (maca64): mex compilation fails due to new Apple linker (_mexCreateMexFunction undefined) #13

@hurak

Description

@hurak

macOS Apple Silicon (maca64): mex compilation fails due to new Apple linker (_mexCreateMexFunction undefined)

Summary

On macOS Apple Silicon (arm64 / maca64) with recent Xcode / Command Line Tools (clang ≥ 17), compilation of Breach MEX files fails with linker errors such as:

Undefined symbols for architecture arm64:
  "_mexCreateMexFunction"
  "_mexDestroyMexFunction"
  "_mexFunctionAdapter"
referenced from: <initial-undefines>

This affects both legacy and online monitoring MEX files (e.g. during InstallBreach, CompileRobusthom, compile_stl_mex).

Root cause

Recent Apple linkers are stricter and no longer tolerate MATLAB’s C++ MEX adapter symbols being left unresolved at link time, even when using -undefined dynamic_lookup.

This is a known incompatibility between:

  • MATLAB’s MEX C++ adapter mechanism, and
  • the new Apple linker shipped with recent Xcode / CLT.

As a result, plain mex fails on macOS arm64, even for trivial examples, unless a workaround is applied.

Workaround (confirmed)

Force the classic Apple linker by adding the following to every mex invocation:

'LDFLAGS=\$LDFLAGS -ld_classic'

Example:

mex file.cpp 'LDFLAGS=\$LDFLAGS -ld_classic'

This resolves the linker errors. A deprecation warning is expected:

ld: warning: -ld_classic is deprecated and will be removed in a future release

Changes required in Breach installation scripts

Any script that constructs mex command strings must explicitly add this flag. In particular:

  • InstallBreach.m
  • CompileRobusthom.m
  • compile_stl_mex.m

Minimal hard-wired fix pattern used successfully:

if ismac && strcmp(computer('arch'),'maca64')
    FLAGS = [FLAGS ' ''LDFLAGS=\$LDFLAGS -ld_classic'' '];
end

or, in scripts using sprintf('mex ...'), inject the same quoted argument into each mex command.

Scope

  • Platform: macOS Apple Silicon (arm64 / maca64)
  • MATLAB: confirmed on R2025b (likely earlier recent releases too)
  • Xcode / CLT: recent versions (clang 17)

Intel macOS, Linux, and Windows are unaffected.

Recommendation for maintainers

Short term:

  • Automatically append -ld_classic to LDFLAGS for ismac && maca64.

Long term:

  • Detect toolchain / linker compatibility more robustly, or
  • Track MathWorks updates addressing this linker incompatibility.

Until then, without this workaround, fresh installs on Apple Silicon macOS will fail.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions