Weird linker error: "building for macOS-x86_64 but attempting to link with file built for macOS-x86_64"

I've got a makefile that creates a library of object files using "ar r mylib.a myobj1.o myobj2.o" etc.

Then I link with "gcc main.o mylib.a -o myexe" and I get the error above.

Clear the error is complete garbage - it's complaining that I'm attempting to link with the architecture I've built for which is what you're supposed to do this isn't an error anyway!

If I link directly with the object files it all works fine, so those objects are OK as is everything else.

Something about putting them in a static library seems to hit a bug in ld therefore - evidenced by the garbage error message.

The only other example I've found of this on google was 'fixed' by the poster using a more up-to-date version of the application he was compiling, so no help in what was the cause nor fix for the error.

Anyone else come across this? Any thoughts on how to debug it further?

Thanks.

Maybe it is caused by something like -mmacosx-version-min, check my post here.

You are likely using two different toolchains unknowingly, one from Apple (installed via Xcode) and one from GNU (installed via Homebrew or MacPorts). If you type ranlib --version and see version info showing that ranlib is GNU, this is the case.

Make sure that /usr/bin comes in your $PATH before /usr/local/bin and /opt/local/bin. When you run which -a ranlib, the first result in the list should be /usr/bin/ranlib. Same for which -a ar-- the first result should be /usr/bin/ar. If it is not so, you need to fix your $PATH.

Once you fix your path and clean your project, try building again and things should work.

I had the same nonsensical error message as the original post, and this answer fixed it immediately.

Weird linker error: "building for macOS-x86_64 but attempting to link with file built for macOS-x86_64"
 
 
Q