开发者

Generate a list of files/objects dynamically from within makefile

开发者 https://www.devze.com 2023-04-07 09:34 出处:网络
I am trying to do this: From a directory, pick all the C (.c) files, generate .o and add it to my final target executable. The C files can be added or removed at anytime, so when I

I am trying to do this:

From a directory, pick all the C (.c) files, generate .o and add it to my final target executable. The C files can be added or removed at anytime, so when I run make for my target, the available C files from the directory has to be picked to compile and link with my target.

So far, I have the following:

define test_tgt = 
DIR = full/path/to/dir
FILES = $(wildcard $(DIR)/*.c)
OBJS = <rule-to-convert-C-to-O>
endef

get_new_files: 
     $(eval $(test_tgt))

final-target: get_new_files 
      $(CC) <other-objs> $(OBJS)

S开发者_C百科omehow this doesn't seem to work. I see a lot of similar examples, but not sure what is wrong here. If this approach is not correct, can anyone suggest a better way to accomplish this.

TIA.


You are trying to program a check that make does by itself. Just list $(OBJS) as dependencies of final-target.

Something like this should work under GNU make:

DIR = full/path/to/dir
FILES = $(wildcard $(DIR)/*.c)
OBJS = $(subst .c,.o,$(FILES))

final-target: $(OBJS)
    $(LD) -o $@ $+  # or similar

Full documentation is here: https://www.gnu.org/software/make/manual/html_node/Text-Functions.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号