2

I was trying to do

ld -r -o test.o lib.a

It worked but test.o ended up being very small file even though lib.a large file.

But if I do

ld -r -o test.o obj1.o obj2.o etc

It seems to work. Just link all object files in lib.a )

Essentially is there a way to tell ld to take all the object files out of .a and link them ?

I’m asking because trying speed up build which creates hundreds of executables but always links with same libraries. Wanted to instead try linking with one big .o file to resolve as much reference as possible before hand. Basically don’t have ld repeat same work each time.

Craig Estey
  • 30,627
  • 4
  • 24
  • 48
steviekm3
  • 905
  • 1
  • 8
  • 19
  • Trying to get object file as the main output. If I do one with linking all object files I get test.o being a ELF LSB relocatable. Same thing if I use the .a but test.o file very small. I want to in next step link test.o with main.o to create executable – steviekm3 Mar 03 '23 at 21:49
  • Does the `ld` command you are using have the `-all_load` switch? – Eric Postpischil Mar 03 '23 at 21:50
  • 1
    For some background, see my answer: [gcc ld: method to determine link order of static libraries](https://stackoverflow.com/a/34168951/5382650) It's tedious to try to force a `.a` to pull in all its `.o` subfiles using `ld`. The usual way is to do: `ar xv mylib.a ; ld -r -o test.r *.o; rm -f *.o ; mv test.r test.o` – Craig Estey Mar 03 '23 at 21:51
  • Okay will check it out. Am using gun linker and Cabot find load-all, also have gold linker – steviekm3 Mar 03 '23 at 21:54
  • Does the `--whole-archive` option help? – John Bollinger Mar 03 '23 at 21:56
  • Yes ! Looks like this is promising – steviekm3 Mar 03 '23 at 21:59
  • Unless the library is of closely interlinked routines so that a program using any of the functions actually ends up using most of them, your proposed method will end up with bigger executables than necessary. – Jonathan Leffler Mar 03 '23 at 23:48

0 Answers0