Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional dependency with make/gmake

Is there a way to direct make/gmake to act upon conditional dependencies?

I have this rule in place:

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
  $(CPPC) -c $(FLAGS_DEV) $< -o $@

In the general case, every .cpp file has a corresponding .h file; however there are a few exceptions. Is there a way to achieve "depend on this if it exists" with gmake? Failing that, is there a best practice for this type of setup?

Thanks in advance; Cheers!

Update: I'm using GCC

like image 963
Chris Tonkinson Avatar asked Dec 11 '25 01:12

Chris Tonkinson


1 Answers

A better way to do this, is to actually determine the dependencies of the cpp files with gcc -MM and include them in the makefile.

SRCS = main.cpp other.cpp
DEPS = $(SRCS:%.cpp=$(DEP_DIR)/%.P)

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
  $(CPPC) -c $(FLAGS_DEV) $< -o $@

$(DEP_DIR)/%.P: $(SRC_DIR)/%.cpp
  $(CPPC) -MM $(FLAGS_DEV) -MT $(OBJ_DIR)/$*.o -MP -MF $@ $<

-include $(DEPS)
like image 184
wich Avatar answered Dec 13 '25 16:12

wich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!