2

I am creating a Makefile of a Keil based project. I have a working Makefile now, but I have manually written rules for all the source files, something like this:

out/abc.o: ../../../src/modules/abc.c
       ARMCC -o $@ $(FLAGS) $^ 
out/def.o: ../../../src/utilities/def.c
       ARMCC -o $@ $(FLAGS) $^ 
out/xyz.o: src/xyz.c
       ARMCC -o $@ $(FLAGS) $^ 

which has become kinda long. The object files need to be in one directory(/out), but the source files are in different levels and in various folders like utilities, modules etc. Is there a way to shorten my Makefile so that it scans these different levels of source files and creates the object files?

EDIT: A follow-up question to the answer. My linker rule is something like this, along with the VPATH addition. I added one directory to VPATH and others are still explicitly compiled.

OBJECT_FILES=out/abc.o out/def.o out/xyz.o

out/binary.axf: $(OBJECT_FILES)
               ARMLINK $(MANY_FLAGS) $^ -o $@
VPATH=../a/b/c/module
out/%.o : %.c
        $(CC) $(C_FLAGS) $(INCLUDE_PATH) -o $@ --depend out/%.d $<

I now get an error that there is no rule for abc.o. abc.c which is present in the directory specified in VPATH under module

*** No rule to make target `out/abc.o', needed by `out/binary.axf'.  Stop.
rookie
  • 1,168
  • 3
  • 14
  • 25
  • That is a different question you should post. But, that means it couldn't find "abc.c" in any of the VPATH paths. You can run `make -d` and see more information about what it's looking for. – MadScientist Mar 07 '18 at 18:09

1 Answers1

3

You can use VPATH for this. It can search a list of directories for source files. Assuming you can come up with the list of directories:

VPATH = ../../../src src

CC = ARMCC

out/%.o : %.c
        $(CC) -o $@ $(CFLAGS) -c $<
MadScientist
  • 92,819
  • 9
  • 109
  • 136
  • Ah, that's definitely helpful! A follow-up question if you dont mind. So I tried this, but since I had explicit rules for each .o which is used to create the binary, now the binary creation rule (that depends on each of these object files) now complains because the explicit rule to create the object file is gone. I am not sure if that is because of this new addition or something else that I am doing incorrectly. – rookie Mar 07 '18 at 05:51
  • @rookie That will be difficult to determine without seeing the rule. I suspect it's because the object files are in `out/` but it's looking in the current directory for them. But that's just a guess. – shawnhcorey Mar 07 '18 at 08:16
  • Agreed. Definitely your rule is wrong but since you don't show either the rule or the exact error message make prints, there's little else we can say. – MadScientist Mar 07 '18 at 15:07
  • I added some content in my question to clarify the problem, I hope that'll help – rookie Mar 07 '18 at 17:54