gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > Compile and link -some- files, and then link more later

#101759 - MrD - Thu Sep 07, 2006 10:52 pm

Say I've got a project consisting of a dozen .c files, which my makefile determines should be compiled into .o files and then linked together and cook ed for 45 until golden brown.

Is there a way to link all of the .o files except one into a single file, which can then be linked to a version of the missing .o and objcopy'd into the final rom?

For example, hypothetical project:
gbamain.c,
levelgfx.c,
enemygfx.c,
enemybehaviour.c,
levelmap.c, and all of the necessary headers.

Can I compile and link gbamain.o, levelgfx.o enemygfx.o and enemybehaviour.o into a single file, and then link that with levelmap.o and complete the rom preparation process?
_________________
Not active on this forum. For Lemmings DS help see its website.

#101762 - gmiller - Thu Sep 07, 2006 11:16 pm

If your makefile dependencies are correct the only thing that will compile are the files that have been changed. I also add the makefile to the dependencies so if the makefile is modified all the code is recompiled.

Using your example here is the makefile I would use (of course I might make a syntax error but the idea will be there:

Code:


#define a rule for creating .o's from .c's
%.o: %.c
   @echo $(notdir $<)
   @echo "'C' THUMB Rule"
   $(CC) -MMD -MP -MF -mthumb -mthumb-interword $(CFLAGS) -c $< -o $@

OFILES = gbamain.o levelgfx.o enemygfx.o enemybehavior.o levelmap.o

game.elf :  makefile $(OFILES)
                 $(LD)  $(LDFLAGS) -specs=gba.specs $(OFILES) $(LIBPATHS) $(LIBS) -o $@

gbamain.o : gbamain.c (all of the header files you care about)

levelgfx.o : levelgfx.c (same with headers you care about)

enemygfx.o : enemygfx.c (headers)

enemybehavior.o : enemybehavior.c (headers)

levelmap.o : levelmap.o (headers)

clean :
          rm -rf $(OFILES)

makefile : clean



If the makefile is newer than the elf all of object files will be removed. If any of the source files are newer than their source or header files that source file will be re-compiled. If you want to override the rule for a source file then put the same line as the rule after the dependency rule and make the changes you need. Remember that the lines that are indented are indented with a TAB not spaces. Hope this helps.

#101763 - tepples - Thu Sep 07, 2006 11:23 pm

MrD wrote:
Say I've got a project consisting of a dozen .c files, which my makefile determines should be compiled into .o files and then linked together and cook ed for 45 until golden brown.

Is there a way to link all of the .o files except one into a single file

Yes, by turning them into a library using ar, the same tool used to build libnds7.a and libnds9.a:
Code:
E:\>arm-eabi-ar
Usage: arm-eabi-ar [emulation options] [-]{dmpqrstx}[abcfilNoPsSuvV] [member-name] [count] archive-file file...
       arm-eabi-ar -M [<mri-script]
 commands:
  d            - delete file(s) from the archive
  m[ab]        - move file(s) in the archive
  p            - print file(s) found in the archive
  q[f]         - quick append file(s) to the archive
  r[ab][f][u]  - replace existing or insert new file(s) into the archive
  t            - display contents of archive
  x[o]         - extract file(s) from the archive
 command specific modifiers:
  [a]          - put file(s) after [member-name]
  [b]          - put file(s) before [member-name] (same as [i])
  [N]          - use instance [count] of name
  [f]          - truncate inserted file names
  [P]          - use full path names when matching
  [o]          - preserve original dates
  [u]          - only replace files that are newer than current archive contents

 generic modifiers:
  [c]          - do not warn if the library had to be created
  [s]          - create an archive index (cf. ranlib)
  [S]          - do not build a symbol table
  [v]          - be verbose
  [V]          - display the version number
  @<file>      - read options from <file>
 emulation options:
  No emulation specific options
arm-eabi-ar: supported targets: elf32-littlearm elf32-bigarm elf32-little elf32-big srec symbolsrec tekhex binary ihex

For more info, man ar.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#101766 - gmiller - Thu Sep 07, 2006 11:40 pm

Yes ... an 'archive' file will put them together in one file. If you put your main in there you might have to force the link step to use it because no one calls main.

#101782 - sajiimori - Fri Sep 08, 2006 12:30 am

Using ar still requires a full link at the end, which works fine, but there's also a "partial link" option which is nice if your link step is getting slow and you're only modifying a small part of the project at a time.

(A full link of a debug build can approach 2 minutes near the end of my projects. Ouch!)

The ld option of interest is -i, for "incremental link".