Is it possible to create an object file from other object files in gcc?

I tried to do something like this in a makefile:

program.exe: ui.o main.o
   gcc ......etc
ui.o: window1.o window2.o
   gcc -c window1.o window2.o -o ui.o #this doesn't want to work
window1.o: window1.c window1.h window1_events.c window1_controls.c ...
   gcc -c window1.c window1_events.c window1_controls.c... -o window1.o
window2.o: ...
   gcc ...
main.o: ...
   gcc ...

but when I compile like this, it gives the error "the input file is not used because the link is not executed", and then I get a bunch of unresolved externs, etc. - problems that are resolved by changing

program.exe: ui.o main.o
   gcc ...

to

program.exe: window1.o window2.o main.o
   gcc ...

so is it possible to simply link the object files together to avoid creating a long line in the makefile and break the build process a bit?

+3
source share
3 answers

- . ar . mylib.a, foo.o bar.o

ar rvs mylib.a foo.o bar.o

, :

gcc -o myexe main.c mylib.a
+8

: , ld -r ld -Ur:

"man ld" Linux:

   -r
   --relocatable
      Generate  relocatable  output---i.e.,  generate  an output file that can
      in turn serve as input to ld.  This is often called partial linking.
      As a side effect, in environments that support standard Unix magic
      numbers, this option also sets the output file’s magic number to
      "OMAGIC".
      If this option is not specified, an absolute file is produced.
      When linking C++ programs, this option will not resolve references to
      constructors;  to do that, use -Ur.

gcc:

gcc -Wl,-r foo.o bar.o -o foobar.o -nostdlib

, , : ( main.c), .

OTOH, , , , , , , window2.c .

+15

To create a library:

ar rvs somelib.a file1.o file2.o file3.o

To link it:

gcc -o program.exe file4.o somelib.a
+3
source

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


All Articles