'Issue with dependency files using Makefiles; file.d exist
I have the following sub-section of makefile that's used to generate the .d files
-include $(wildcard $(patsubst %,$(OBJ_PATH)/%.d,$(basename $(SRCS))))
%.o: %.cpp
@$(CC) -I$(INCLUDE_PATH) $(CFLAGS) $< -MM -MT $(@:.d=.o) > $(OBJ_PATH)/$(notdir $*.d)
${CC} -I$(INCLUDE_PATH) $(CFLAGS) -c $< -o $(OBJ_PATH)/$(notdir $@)
Sometimes when I build the project, I get the following error /bin/sh: /path/to/my/build/dir/file.d: File exists, although .d files does not exist.
Then I keep getting this error, the only way to get rid of it is to remove the whole enclosing directory and use git restore and the system will build successfully.
System info:
- git version 2.27.0
- GNU Make 4.2.1
- Red Hat Enterprise Linux 8.5 (Ootpa)
- Free inodes: 426798634
Solution 1:[1]
You should remove the @ from your line that creates the .d file, so you can see what the command line actually is. It's always a bad idea to add @ before your makefile is working 100% correctly. Then you could cut and paste a full failure example into your question, including the command that generated the error message.
Your build lines are not right. During the compilation, you need to use $@ not $(OBJ_PATH)/$(notdir $@). It's always wrong to build a file that is not exactly $@.
During the creation of the dependency file $(@:.d=.o) is useless because $@ is already set to xxx.o so changing the .d suffix to .o doesn't do anything. You should just use -MT $@ here.
You can replace $(OBJ_PATH)/$(notdir $*.d) with the simpler %*.d.
This error is being shown by the shell and there's really no way we can understand what the problem is with the info given. Why would the shell give a "File exists" error when you use ">" to overwrite it?
I have a suspicion that it's not actually this command that is generating that error.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | MadScientist |
