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


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?



Solution 1:[1]

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.

Solution 2:[2]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 George Morgan