简体   繁体   中英

ld: building for macOS-x86_64 but attempting to link with file built for macOS-x86_64

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

The issue was solved when I directly put those object files rather than gathering them into a static library, ie, 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.

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.

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.ho ).

Removing those fixed the issue.

I found out some archive were included in my repo

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

solved my issue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM