2

I have this strange issue where creating / using a static library works in my Ubuntu VM but not on macOS:

ld: warning: ignoring file ./dist/libXXXX.a, building for macOS-x86_64 but attempting to link with file built for macOS-x86_64

Command to create the static library is:

ar rcs libtest.a obj1.o obj2.o ...

Compiler invocation:

gcc -g -Wall -Wextra main.c -L./dist -lXXXX -o main

Searching on google didn't yield any usable results except for this (maybe) related question on SO:

Possible related question

4 Answers 4

1

The issue was solved when I directly put those object files rather than gathering them into a static library, i.e., gcc -g -Wall -Wextra main.c obj1.o obj2.o -o main

After that, I got many warnings like ld: warning: object file (obj1.o) was built for newer macOS version (11.0) than being linked (10.14), but it is a warning, and the object is linked, so the problem is solved.

The root cause is that some library passes -mmacosx-version-min=10.14 to gcc, so the object file is built for 10.14, but my macos is now 11.0.

If you want to make things work, try directly using object files rather than creating a static library.

If you want to resolve all the warnings, find ``-mmacosx-version-min` and comment it.

1

I realize this is an old post and you found your fix, but let me post this here for anyone else who runs into this problem for whom these answers don't provide a solution.

You might be using two different toolchains unknowingly, one from Apple (installed via Xcode) and one from GNU (installed via Home-brew 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.

1
  • 1
    That's good! I can't verify it because as you correctly stated I was able to solve my problem. However your effort is appreciated, so here have my upvote :)
    – marco-a
    Jul 7 at 23:39
0

After looking at my script that automatically creates the static library I've found the culprit:

For some reason my tool created object files for header files (resulting in files like header.h.o).

Removing those fixed the issue.

0

I found out some archive were included in my repo

rm -rf ./**/*.a
rm -rf ./**/.o
make

solved my issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.