warning: x86_64-apple-darwin17-ranlib: archive library: lib.a the table of contents is empty (no object file members in the library define global symbols)
The final linking step fails with:
undef: _sum
Undefined symbols for architecture x86_64:
"_sum", referenced from:
_main in lto.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
...which is to be expected after the earlier ranlib warning about the static library. The same steps using the native toolchain clang, ar and ranlib tools on macOS result in a working binary with LTO correctly applied (so without a mul symbol).
Then I tried directly using llvm-ar and llvm-ranlib (because the .o files contain LLVM bitcode) from my Ubuntu host system:
This way I do not get a warning from llvm-ranlib but the final linking step still fails:
ld: warning: ignoring file lib.a, building for macOS-x86_64 but attempting to link with file built for unknown-unsupported file format ( 0x21 0x3C 0x61 0x72 0x63 0x68 0x3E 0x0A 0x2F 0x20 0x20 0x20 0x20 0x20 0x20 0x20 )
undef: _sum
Undefined symbols for architecture x86_64:
"_sum", referenced from:
_main in lto.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Probably because ld64 from osxcross does not recognize the archive produced by llvm-ar. At this point I'm not sure which combination of tools/linkers should be used to achieve the desired result.
I have built
osxcross
on an Ubuntu 20.04 system with thellvm-dev
andlld
packages installed using:For my test, I created three files
lib.h
,lib.c
andmain.c
:lib.h
:lib.c
:main.c
:This works and results in a binary without the
mul
function, as expected:This also works and results in a binary with the (unused)
mul
function:Trying to combine a static library and LTO fails:
Here
ranlib
produces this warning:The final linking step fails with:
...which is to be expected after the earlier
ranlib
warning about the static library. The same steps using the native toolchainclang
,ar
andranlib
tools on macOS result in a working binary with LTO correctly applied (so without amul
symbol).Then I tried directly using
llvm-ar
andllvm-ranlib
(because the.o
files contain LLVM bitcode) from my Ubuntu host system:This way I do not get a warning from
llvm-ranlib
but the final linking step still fails:Probably because
ld64
fromosxcross
does not recognize the archive produced byllvm-ar
. At this point I'm not sure which combination of tools/linkers should be used to achieve the desired result.