1

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?

FuzzyBall
  • 75
  • 6
  • 1
    What does `file libmyLibrary.a` report? – Paul Sanders Feb 27 '22 at 15:26
  • makefile executes `ar -r /path/to/lib/libmyLibrary.a src/stl.hpp.gch ` -> `ar: creating archive /path/to/lib/libmyLibrary.a` – FuzzyBall Feb 27 '22 at 18:11
  • `ar -t libmyLibrary.a` lists: `__.SYMDEF, stl.hpp.gch, all the .o files` – FuzzyBall Feb 27 '22 at 18:14
  • `file libmyLibrary.a` reports `libmyLibrary.a: current ar archive` – FuzzyBall Feb 27 '22 at 18:16
  • 1
    Rats, not what I was hoping for. Do you still have the `.o` files lying around? If so, running `file` on one of those will tell you what architecture it contains. If not, you can extract them from your library with `ar` and then do it. – Paul Sanders Feb 27 '22 at 20:25
  • `file SomeFile.o` tells me `SomeFile.o: Mach-O 64-bit object arm64` for .o files from the library – and `main.o: Mach-O 64-bit object arm64` for the executable and all its .o files. It also prints `stl.hpp.gch: data` for my precompiled header, which I guess is correct. – FuzzyBall Feb 27 '22 at 21:42
  • 1
    Hmmm, looks fine. I think you should show us your Makefile. I don't use make personally, but I'm sure there are people here who do. And of course I have to ask you: why not use Xcode? I'm doing pretty much what you're doing (although in my case I'm building a fat binary) and it works OK. – Paul Sanders Feb 27 '22 at 21:45
  • Thanks anyways, I'll keep digging a little deeper. I'd just keep using XCode but the library is used by an executable running on a raspberry pi, as well as my Mac, so I thought a makefile would work best. – FuzzyBall Feb 27 '22 at 22:00

2 Answers2

6

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.

George Morgan
  • 669
  • 10
  • 23
  • This is the right answer. The `ar` program in your system must be broken if you see this message. I ran `brew install llvm@12` and then used `llvm-ar` instead of `ar` and the error went away – Carlos Granados Jun 12 '22 at 17:01
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.

FuzzyBall
  • 75
  • 6