5 ms·
Pretty spot on. I use a pretty similar dependency system for all of my C and C++ projects. FLAGS += -MMD CFLAGS += $(FLAGS) CXXFLAGS += $(FLAGS)
by vortico 9y ago
Pretty spot on. I use a pretty similar dependency system for all of my C and C++ projects.
FLAGS += -MMD
CFLAGS += $(FLAGS)
CXXFLAGS += $(FLAGS)
OBJECTS += $(patsubst %, build/%.o, $(SOURCES))
DEPS = $(patsubst %, build/%.d, $(SOURCES))
$(TARGET): $(OBJECTS)
$(CXX) -o $@ $^ $(LDFLAGS)
-include $(DEPS)
build/%.c.o: %.c
@mkdir -p $(@D)
$(CC) $(CFLAGS) -c -o $@ $<
build/%.cpp.o: %.cpp
@mkdir -p $(@D)
$(CXX) $(CXXFLAGS) -c -o $@ $<
- babuskov 9y agoThis looks very similar to the Makefile I'm using except that beside -MMD I also have -MP flag. I created this Makefile a couple years back and I'm reusing it for every new project, but I cannot recall really why I needed to add -MP.
- vortico 9y agohttps://gcc.gnu.org/onlinedocs/gcc-4.8.0/gcc/Preprocessor-Options.html https://gcc.gnu.org/onlinedocs/gcc-4.8.0/gcc/Preprocessor-Op... Looks useful, I'll add it to my configs.
- babuskov 9y agoYes, that's it. Removing a .h file from your project would trigger errors without that switch. I looked at clang manual when I searched for it, but unlike GCC it does not explain it.
- cozzyd 9y agoWow, that -MMD flag is very handy. Can't believe I didn't know about it until now.
- v_lisivka 9y agoHow to use it?
- vortico 9y agoSave the file to something like `compile.mk` and create a Makefile with SOURCES += $(wildcard src/*.cpp) SOURCES += other_files.c TARGET = foo include compile.mk
- abainbridge 9y agoFor all these years I've been _replacing_ the .c and .cpp extension with .o in the object file name. How could I have been so wrong?!
- vortico 9y agoThere's no problem with replacing, other than losing the ability to glance at an object and determine which language it came from, and name conflicts with `util.c` and `util.cpp` in the same directory (believe me, it happens).