0

I'm new to the linking and compilation process (if you've got good learning sources, please share!), and am playing around with makefiles. I apologize in advanced that the following sounds obvious.

Say I compile 2 .o files, and then link them. Then, I compile another .o file. Can I link the initial result with the third object file?

Fine Man
  • 455
  • 4
  • 17
  • You compile all files, and then link them. Linking is the final step. You can compile object files and add then to a library (.lib) file, but it's still only used after all compilation is done. – Ken White Jan 22 '17 at 04:24
  • There are several possibilities here, any or even all of which might be irrelevant to your actual needs. What you mean by "Say I compile 2 .o files, and then link them" is unclear. Please post specimen commandlines or `make` recipes that show what you mean by this. – Mike Kinghan Jan 22 '17 at 19:34

1 Answers1

0

Is it Possible: Link object files, then link result to more object files?

Yes:

ld -r -o foo-combined.o foo1.o foo2.o
gcc foo-combined.o bar.o -o a.out

The -r flag creates a relocatable (object) file, which effectively merges foo1.o and foo2.o.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362