liv2hak
liv2hak

Reputation: 14970

Including generated files in Makefile dependencies

I have a set of files that get generated.

GENERATED = log/loga.c log/logb.h include/loga.h

My target below initproc depends on GENERATED above. But I can't included $(GENERATED) below like $(INIT_OBJS). It says

fatal error: include/loga.h: No such file or directory when I do make initproc

initproc: $(INIT_OBJS) log/loga.o init/initb.o settings/settingc.o
             $(CXX) $(CXXFLAGS)  $(LDFLAGS) $^ $(LDLIBSXX)  -o $@

How do I include the dependency above ?

Upvotes: 0

Views: 1725

Answers (1)

jmlemetayer
jmlemetayer

Reputation: 4952

You have to add a $(GENERATED) target with some rules to explain how to generate this files. Moreover you have to manage header, source and object files by using some patsusbt and filter functions. Something like that should be working:

# All the generated files (source and header files)
GENERATED := log/loga.c log/logb.h include/loga.h

# The rules to generate this files
$(GENERATED):
        <some commands to generate the files ...>

# The rules to generate object files from the generated source files
# This could be merge with another rule from your Makefile
GENERATED_SRC := $(filter %.c,$(GENERATED))
GENERATED_OBJ := $(patsubst %.c,%.o,$(GENERATED_SRC))

$(GENERATED_OBJ): $(GENERATED_SRC)
        $(CXX) $(CXXFLAGS) -c $^ -o $@

# The final target depends on the generated object files
initproc: $(INIT_OBJS) <other objects ...> $(GENERATED_OBJ)
        $(CXX) $(CXXFLAGS) $(LDFLAGS) $^ $(LDLIBSXX) -o $@

Upvotes: 2

Related Questions