combine two GCC compiled .o object files into a third .o file
Passing -relocatable
or -r
to ld
will create an object that is suitable as input of ld
.
$ ld -relocatable a.o b.o -o c.o
$ gcc c.o other.o -o executable
$ ./executable
The generated file is of the same type as the original .o
files.
$ file a.o
a.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped
$ file c.o
c.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped
If you want to create an archive of two or more .o files (i.e.. a static library) use the ar
command:
ar rvs mylib.a file1.o file2.o