Home > Back-end >  What is the best way to have different configurations compile with different preprocessor flags on t
What is the best way to have different configurations compile with different preprocessor flags on t

Time:07-20

So say I have something like the following in a makefile.def:

MODLIST = src/main
CPPFLAGS = -DCFG0=0 -DCFG1=1
cfg0.elf: CPPFLAGS  = -DCFG=CFG0
cfg0.elf: $(addprefix $(ODIR)/, $(addsuffix .o, $(MODLIST)))
cfg1.elf: CPPFLAGS  = -DCFG=CFG1
cfg1.elf: $(addprefix $(ODIR)/, $(addsuffix .o, $(MODLIST)))

and we have a dummy main.c with the following:

#if CFG == CFG0
printf("CFG0");
#elif CFG == CFG1
printf("CFG1");
#endif

The issue seems to be since they are being overwritten during compilation, (at least what I had thought but could be wrong) when both configurations get to that statement, they both print out the same "CFG0" in this case.

A couple ideas I had but ran into issues while trying to link...

  1. Append something to the suffix like

cfg0.elf: $(addprefix $(ODIR)/, $(addsuffix _cfg0.o, $(MODLIST)))

  1. Append something to the output directory like

cfg0.elf: $(addprefix $(ODIR)/CFG0/, $(addsuffix .o, $(MODLIST)))

I tried to modify the LDFLAGS but I didn't find a way to get it to compile correctly. Not sure if I was heading in the right direction or if there is a better way to go about something like this.

CodePudding user response:

Be aware when you using option -DCFG=xxx if the output .o are in same position because:

  • cfg0.elf require all .o files in compile with -DCFG0=0
  • cfg1.elf require all similar .o files but compiler detect that it was compiled already event with -DCFG0=0 (because depend does not apply over compiler option. Then the cfg1.elf is linked with same file with cfg0.elf.

Your idea about Append something to the suffix or Append something to the output directory are all the right approach. Actually, I prefer Append something to the output directory. If you have problem with linking, I think you should raise question specifically for your linking issue.

  • Related