Creating a static library with multiple architectures in C

I am working on Ubuntu and I have created a static c library following the instructions of this site .

But the resulting .a package only works on the machine where it was compiled.

I know that the .a archive contains object files (only one in my case), so I can pack together object files compiled on different machines (i386 and amd64) so ​​that GCC can know which file should use

If I can’t, can at least make my library recognizable by other similar machines? (using -L. -llibraryname)

Example:

archive name "libvisualt64.a"

: gcc -o main main.c -L. -lvisualt64

He speaks:

skipping incompatible ./libvisualt.a when searching for -lvisualt64
cannot find -lvisualt64
error: ld returned 1 exit status

In this case, I collected the source and built the archive on the same 64-bit machine only yesterday. And yesterday everything went fine. This also happens on 32-bit machines.

+4
source share
1 answer

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 problem using your library on another computer of the same architecture. The only potential problem may be related to dependencies.

, , FatELF... :

FatELF .


, , libvisualt64.a , gcc...

gcc target:

ar xv ${STATIC_LIBRARY}
file *.o
gcc -v 2>&1 | grep '^Target: '

() :

test.o: ELF 64-bit LSB  relocatable, x86-64, version 1 (SYSV), not stripped
Target: x86_64-linux-gnu
+1

Source: https://habr.com/ru/post/1673609/


All Articles