Gnu Makefile - Handling dependencies

后端 未结 12 2539
滥情空心
滥情空心 2021-02-07 20:24

What approach do C++ programmers on Unix platform use to create and manage Makefiles?

I was using hand made Makefiles for my projects but they don\'t handle header file

12条回答
  •  旧巷少年郎
    2021-02-07 21:03

    gcc/g++ can generate dependencies for you with the -M family of options. The following works by specifying how to generate .depends files given a source file. By doing -include $(DEPS) $(DEPS) is recognized as a target and will be built/rebuilt when the source files change.

    CXX      = g++
    CXXFLAGS = -Wall -O3
    LDFLAGS  =
    
    TARGET = testcpp
    SRCS   = main.cc x.cc foo.cc
    OBJS   = $(SRCS:.cc=.o)
    DEPS   = $(SRCS:.cc=.depends)
    
    
    .PHONY: clean all
    
    all: $(TARGET)
    
    $(TARGET): $(OBJS)
            $(CXX) $(CXXFLAGS) $(LDFLAGS) $(OBJS) -o $(TARGET)
    
    .cc.o:
            $(CXX) $(CXXFLAGS) -c $< -o $@
    
    %.depends: %.cc
            $(CXX) -M $(CXXFLAGS) $< > $@
    
    clean:
            rm -f $(OBJS) $(DEPS) $(TARGET)
    
    -include $(DEPS)
    

提交回复
热议问题