Cannot link library for macOS-arm64 with executable for macOS-arm64

1.9k views Asked by At

I have some trouble with creating a build system on a **Monterey M1 MacBook**:

So far I have a working Makefile to build and link a library.
(simplified: g++ -c all .cpp files into .o files → ar -r <.o files> libmyLibrary.a>
works great

THE PROBLEM:
When I try to build an executable binary that uses said libmyLibrary.a. The compilation of source files works fine, but I get the following (seemingly nonsensical) linker warning:

ld: warning: ignoring file /Path/to/lib/libmyLibrary.a,
building for macOS-arm64 but attempting to link with file built for macOS-arm64

→ I ofc then get some Undefined symbols for architecture arm64: ... <stuff from library> referenced from: <stuff from executable>

How can building for the same target as the library be a problem?

2

There are 2 answers

1
FuzzyBall On

I managed to compile the executable after changing how I link my library. My Makefile also linked a precompiled header .pch file.

After removing that, it worked fine.

1
George Morgan On

This is caused by having an incompatible version of ar installed in your system. ar in homebrew binutils seems to be broken. This can be resolved by symlinking llvm-ar over ar, or using the system ar.