简体   繁体   English

c ++ makefile未检测到.h文件中的更改

[英]c++ makefile not detecting the changes in the .h files

I have a longish make file and the part where it compiles my source code files is here我有一个很长的 make 文件,它编译我的源代码文件的部分在这里

$(MAINFILE):    $(BINDIR) $(OBJDIR) $(SCIPLIBFILE) $(LPILIBFILE) $(NLPILIBFILE) $(MAINOBJFILES)
        $(LINKCXX) $(MAINOBJFILES) $(LINKCXXSCIPALL) $(LDFLAGS)  -I$(GRBPATH)/mac64/include -L$(GRBPATH)/mac64/lib -lgurobi_c++ -lgurobi81 $(LINKCXX_o)$@

$(OBJDIR)/%.o:  $(SRCDIR)/%.cpp  
        $(CXX) $(FLAGS) $(OFLAGS) $(BINOFLAGS) $(CXXFLAGS) -I$(GRBPATH)/mac64/include -c  $< $(CXX_o)$@

The header file changes take effect only when I clean everything and then compile from scratch.只有当我清理所有内容然后从头开始编译时,头文件更改才会生效。 I have tried this:我试过这个:

$(MAINFILE):    $(BINDIR) $(OBJDIR) $(SCIPLIBFILE) $(LPILIBFILE)   $(NLPILIBFILE) $(MAINOBJFILES)
            $(LINKCXX) $(MAINOBJFILES) $(LINKCXXSCIPALL) $(LDFLAGS)  -I$(GRBPATH)/mac64/include -L$(GRBPATH)/mac64/lib -lgurobi_c++ -lgurobi81 $(LINKCXX_o)$@

$(OBJDIR)/%.o:  $(SRCDIR)/%.cpp   (SRCDIR)/%.hpp  (SRCDIR)/%.h
        $(CXX) $(FLAGS) $(OFLAGS) $(BINOFLAGS) $(CXXFLAGS) -I$(GRBPATH)/mac64/include -c  $< $(CXX_o)$@ 

But I was getting the error:但我收到了错误:

make: *** No rule to make target `obj/static/O.darwin.x86_64.gnu.opt/ColG_main.o

I am a bit new to makefiles and I was wondering if I can fix this issue.我对 makefile 有点陌生,我想知道我是否可以解决这个问题。

make knows only about the dependencies you designate in your makefile. make只知道您在 makefile 中指定的依赖项。 In general then, the solution to this problem is to name the appropriate headers as additional prerequisites for the object files.通常,此问题的解决方案是将适当的标头命名为目标文件的附加先决条件。 That's what this rule appears to be trying to do ...这就是这条规则似乎试图做的......

 $(OBJDIR)/%.o: $(SRCDIR)/%.cpp (SRCDIR)/%.hpp (SRCDIR)/%.h $(CXX) $(FLAGS) $(OFLAGS) $(BINOFLAGS) $(CXXFLAGS) -I$(GRBPATH)/mac64/include -c $< $(CXX_o)$@

, but there are both syntactic and semantic problems with it. ,但它同时存在句法和语义问题。

Syntactically, you've omitted two $ s from expressions that are intended to be variable references, (SRCDIR) instead of $(SRCDIR) .从语法上讲,您从旨在作为变量引用的表达式中省略了两个$(SRCDIR)而不是$(SRCDIR)

That's easy enough to fix, but it's merely a feeder into the main problem: the (corrected) rule applies only to building object files that have a corresponding .cpp and a corresponding .hpp and a corresponding .h that either exist or can themselves be built.这很容易修复,但它只是主要问题的一个馈线:(更正的)规则仅适用于构建具有相应.cpp相应.hpp以及相应.h目标文件,这些文件要么存在,要么本身可以是建成。 Perhaps that's really the case for all the objects you want to build, but in my experience, it is atypical for a C++ source file to have two corresponding headers.也许对于您要构建的所有对象来说,情况确实如此,但根据我的经验,C++ 源文件具有两个相应的标头是不典型的。 Except where you have some other rule applicable for building an object, make will report an error such as you observed for objects that don't have both corresponding headers除非您有其他一些适用于构建对象的规则,否则make将报告一个错误,例如您对没有两个相应标头的对象观察到的错误

Moreover, if any of your object files depend on headers other than those directly corresponding to them then that rule does not capture those dependencies.此外,如果您的任何目标文件依赖于与它们直接对应的标头以外的标头,则该规则不会捕获这些依赖项。 Therefore, it will not reliably produce rebuilds of all objects affected by a given header change.因此,它不会可靠地生成受给定标头更改影响的所有对象的重建。

The solution is to correctly specify all the dependencies for each target.解决方案是正确指定每个目标的所有依赖项。 You probably cannot do this via your pattern rule, but you can provide addtional, prerequisite-only rules to specify the header dependencies.您可能无法通过模式规则执行此操作,但您可以提供附加的、仅限先决条件的规则来指定标头依赖项。 You can write such rules manually, but if you have a lot of targets or if their dependency lists change frequently then that is painful and error-prone to write and maintain.您可以手动编写此类规则,但是如果您有很多目标,或者它们的依赖项列表经常更改,那么编写和维护会很痛苦且容易出错。 If you're using a toolchain that supports it, however, then there are ways to generate those dependency rules automatically.但是,如果您使用支持它的工具链,则可以通过多种方法自动生成这些依赖项规则。 There are lots of online resources discussing that, such as有很多在线资源对此进行了讨论,例如

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM