I'm working on Ubuntu and I've made a static c library following the instructions on this site.
But the resulting .a package works only on the machine where it has been compiled.
I know that an .a archive contains object files (only one in my case), so, can I package together object files compiled in different machines (i386 and amd64) in a way so GCC can know which file should be using?
If I cannot, can at least make my library recognizable by other same-arch machines?
(using -L. -llibraryname
)
Example:
archive name "libvisualt64.a"
command: gcc -o main main.c -L. -lvisualt64
Says:
skipping incompatible ./libvisualt.a when searching for -lvisualt64 cannot find -lvisualt64 error: ld returned 1 exit status
In this case I compiled the source and built the archive in the same 64bit machine just yesterday. And yesterday it worked fine. This happens on 32bit machines too.
Linux uses an executable file format called ELF. An ELF file can only contain the necessary machine code for a single architecture.
There should be no problems using your library on another machine of the same architecture. The only potential issue could be related to dependencies.
If you really want a single binary to support multiple architectures, then you may be interested in FatELF... Though it is not supported without a kernel patch:
Up to now the FatELF is not integrated in the kernel mainline.
To address your edit, it is likely that your libvisualt64.a
is not built for the same architecture that gcc
is targeting...
You'll need to extract the object files from the archive, and compare them with gcc
's target:
ar xv ${STATIC_LIBRARY}
file *.o
gcc -v 2>&1 | grep '^Target: '
I see the following (compatible) outputs:
test.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
Target: x86_64-linux-gnu