0

I'm trying to make a relocatable object file with gcc. I use solution from this post. The solution works fine with ld:

$ ld -r a.o b.o -o c.o

However when I try to use it with gcc, the following error happens:

$ gcc -r a.o b.o -o c.o
/usr/bin/ld: cannot find -lgcc_s
/usr/bin/ld: cannot find -lgcc_s
collect2: ld returned 1 exit status

Using the -Wl,-r and -Wl,--relocatable options gives the same result.

Is there any way to link relocatable object file with gcc or I'm forced to use ld for doing this?

Community
  • 1
  • 1
anton_rh
  • 8,226
  • 7
  • 45
  • 73
  • 1
    You need to tell gcc not to use any standard libraries. Frankly I don't understand the need. Use ld, that's what it's for. – n. m. could be an AI Nov 18 '15 at 10:00
  • I'm composing a command that makes output file from the set of object files. I want to make it universal, so it can produce executables, shared libraries and relocatable objects depending on output filename extension. I use _g++_ for making executables and shared libraries, wanted to use it for making relocatable objects too. And I'm not sure that extra _gcc_ flags passed as parameter to my command will be valid for _ld_. – anton_rh Nov 18 '15 at 10:13
  • Your universal solution has to select program options depending on the extension. Might select the program itself as well. `gcc` is as different from `ld` as from `gcc -shared`. – n. m. could be an AI Nov 18 '15 at 10:35

1 Answers1

1

To solve this problem, the -nostdlib option must also be passed to gcc:

$ gcc -r -nostdlib a.o b.o -o c.o

I don't know it for sure, but it seems without this option gcc tries to link standard libraries into output relocatable object.

anton_rh
  • 8,226
  • 7
  • 45
  • 73