Conditional dependency with make / gmake

Is there a way to make make / gmake affect conditional dependencies?

I have this rule:

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

In general, each .cpp file has a corresponding .h file; however, there are a few exceptions. Is there a way to achieve “depends on it if it exists” with gmake? Otherwise, is there a best practice for this type of setup?

Thanks in advance; Hooray!

Update: I am using GCC

+3
source share
3 answers

The best way to do this is to actually determine the dependencies of the cpp files with gcc -MM and include them in the make file.

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)
+3
source

makefile, gmake. -, gmake , ; -, , ( , ). , makefile :

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

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

gmake , .h, , . , , (, "foo.o" , "foo.h" ).

, ; :

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

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
        $(COMPILE)
+3

gcc . () .d , , , .h .cpp, .cpp .h, .

.

DEPFILES=$(patsubst $(SRC_DIR)/%.cpp,$(OBJ_DIR)/%.d,$(wildcard $(SRC_DIR)/*.cpp))

$(OBJ_DIR)/%.d: $(SRC_DIR)/%.cpp
    g++ -MF $@ -MM -MT $@ -MT $(basename $@).o %<

include $(DEPFILES)

, .d, .o make -MT, .

0
source

Source: https://habr.com/ru/post/1731516/


All Articles