如何确保我的makefile检测到我的头文件和cpp文件中的更改? [英] How do I ensure my makefile detects changes in my header and cpp files?

查看:316
本文介绍了如何确保我的makefile检测到我的头文件和cpp文件中的更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,每当我执行make我的makefile告诉我

Currently, whenever I do make my makefile tells me

make:`some/obj/file.o'是最新的.

make: `some/obj/file.o' is up to date.

无论我是否编辑了生成该目标文件所涉及的任何文件.如何使其检测变化?这是一个重现该问题的简单makefile:

regardless of whether I've edited any of the files involved in generating that object file. How do I make it detect changes? Here is a simple makefile that reproduces the problem:

SHELL := /bin/bash
src := src
sources := $(shell find $(srcDir) -name "*.cpp")                                
objects := $(sources:%.cpp=%.o)                                                 

-include $(sources:%.cpp=%.d)                                                   

all: prog                                                                       

prog:   $(objects)                                                              
    g++ $(objects) -o /a.out                                                    

%.o: %.cpp                                                                      
    $(CXX) $(CXXFLAGS) -MMD -MP -c $< -I $(srcDir) -o $@                        

clean:                                                                          
    find $(srcDir) -type f -iname "*.o" -delete                                 
    find $(srcDir) -type f -iname "*.d" -delete 

当前,我必须每次都运行make clean来重新编译它,这显然不理想!

currently I have to run make clean everytime to get it to recompile, which obviously isn't ideal!

这是我根据Chnossos的回答进行的尝试:

EXE := a.out                                                                    
SRCDIR := src                                                                   
SRC := $(shell find $(srcDir) -name "*.cpp")                                    
DIR := .obj                                                                     
OBJ := $(SRC:%.cpp=$(DIR)/%.o)                                                  
DEP := $(OBJ:.o=.d)                                                             

CXXFLAGS += -std=c++11                                                          
CXXFLAGS += -I /home/arman/lib/eigen-eigen-6b38706d90a9                  
CXXFLAGS += -I /home/arman/lib/boost_1_55_0                              
CXXFLAGS += -I /home/arman/lib/lodepng/                                  
CXXFLAGS += -L /home/arman/lib/boost_1_55_0/stage/lib                    
CPPFLAGS += -MMD -MP                                                            

.PHONY: all clean                                                               

-include $(DEP)                                                                 

all: $(EXE)                                                                     

$(EXE):   $(OBJ)                                                                
        $(CXX) $(OBJ) -o $@                                                     

$(DIR)/%.o: %.cpp                                                               
        $(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ -c $< -I $(SRCDIR)                 

clean:                                                                          
        $(RM) -f $(DIR)                                                         

我现在收到以下错误:

src/core/file1.cpp:839:1:致命错误:打开依赖文件.obj/./src/core/file1.d:没有此类文件或目录

src/core/file1.cpp:839:1: fatal error: opening dependency file .obj/./src/core/file1.d: No such file or directory

请注意,我具有以下目录结构:

Note that I have the following directory structure:

/prog/makefile->生成文件 /prog/dir1/->一些cpp/hpp文件 /prog/dir2/->更多cpp/hpp文件 /prog/->这里也有一些cpp/hpp文件

/prog/makefile -> The makefile /prog/dir1/ -> some cpp/hpp files /prog/dir2/ -> more cpp/hpp files /prog/ ->there are some cpp/hpp files here too

我有很多文件夹(不仅仅是dir1dir2),所以我不想每次都指定它们.

I have many folders (more than just dir1 and dir2) so I'd like to not have to specify them each time.

推荐答案

SRC         := $(wildcard *.cpp)
OBJ         := $(SRC:.cpp=.o)
DEP         := $(OBJ:.o=.d)

CPPFLAGS    += -MMD -MP -I.

.PHONY: all clean

all: prog

prog:   $(OBJ)
    $(CXX) $(LDFLAGS) $^ $(LDLIBS)

-include $(DEP)

clean:
    $(RM) $(OBJ) $(DEP)

您还可以稍作努力将.o.d文件编译到这样的隐藏文件夹中:

You can also with a little efforts compile your .o and .d files into a hidden folder like this :

EXE         := a.out
SRC         := $(wildcard *.cpp)
DIR         := .obj
OBJ         := $(SRC:%.cpp=$(DIR)/%.o)
DEP         := $(OBJ:.o=.d)

CPPFLAGS    += -MMD -MP -I.

.PHONY: all clean

all: $(EXE)

$(EXE):   $(OBJ)
    $(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@

$(DIR)/%.o: %.cpp | $(DIR)
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ -c $<

$(DIR):
    @mkdir $@

-include $(DEP)

clean:
    $(RM) -r $(DIR)


编辑:这是我尝试进行的修改:


EDIT: Here is my attempt for your edit :

一些简短的说明,$(LDLIBS)用于-l标志,而$(LDFLAGS)用于-L标志.

Some quick note, the $(LDLIBS) is here for your -l flags, whereas $(LDFLAGS) is meant for -L flags.

SRCDIR := src
OBJDIR := .obj

EXE := a.out
SRC := $(shell find $(SRCDIR) -name "*.cpp")
OBJ := $(SRC:$(SRCDIR)/%.cpp=$(OBJDIR)/%.o)
DEP := $(OBJ:.o=.d)

LDFLAGS += -L /home/arman/lib/boost_1_55_0/stage/lib
CXXFLAGS += -std=c++11
CPPFLAGS += -I $(SRCDIR)
CPPFLAGS += -I /home/arman/lib/eigen-eigen-6b38706d90a9
CPPFLAGS += -I /home/arman/lib/boost_1_55_0
CPPFLAGS += -I /home/arman/lib/lodepng/
CPPFLAGS += -MMD -MP

.PHONY: all clean

all: $(EXE)

$(EXE): $(OBJ)
    $(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@    

.SECONDEXPANSION:
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp | $$(@D)/
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ -c $<

%/:
    mkdir $@

-include $(DEP)

clean:
    $(RM) -r $(OBJDIR)

如果有什么缺失,请告诉我.

Tell me if something is missing.

这篇关于如何确保我的makefile检测到我的头文件和cpp文件中的更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆