0

I need to create a static library using source files in different directories. I am able to create static library by specifying each and every source file in the Makefile. It increased more content in the Makefile. Let me explain what I did with an example:

#Specifying the each and every source files to FILES is more complex to me. Trying to find out alternative to this problem

Files = Source1\A.c Source\B.c Source2\C.c Source3\D.c ........... Sorce3/Z.c

$(OUT_FILE_NAME): $(patsubst %.c,%.o,$(wildcard $(FILES)))
    ar -r -o $@ $^

#Compiling every *.c to *.o
%.o: %.c dirmake
    arm-none-eabi-gcc -c -o $@  $<

I'm trying to explore alternative way to specifying all source files in the Makefile. This will help us to not modify this Makefile again if any new source file added. Is it possible to do that? I tried to search for this problem and many sources explain how to create static library but I don't see any alternative solution for this problem.

1 Answers1

0

Without knowing the details of your development environment I would try these minimal changes first. Using the wildcard-function for the source directories will enable you to add more sources without having to edit the Makefile every time. Just keep the directories clean of any unused c-files to avoid including stuff in your library that you don't want.

I have also made the Files variable simply expanded (evaluated once) by using := for assignment. I also simplified the prerequisite of the first rule and fixed the case of the variable-name.

Files := $(wildcard Source1/*.c) $(Source2/*.c) $(Source3/*.c)

$(OUT_FILE_NAME): $(patsubst %.c,%.o,$(Files))
    ar -r -o $@ $^

#Compiling every *.c to *.o
%.o: %.c dirmake
    arm-none-eabi-gcc -c -o $@  $<
krueger
  • 211
  • 3
  • 4
  • A more versatile function to retrieve file or directory names can be found here: https://github.com/markpiffer/gmtt#call-wildcard-reclist-of-globs – Vroomfondel Jan 04 '22 at 11:22