在什么情况下需要定位.h文件

Lea*_*cim 1 c++ makefile

在我看到的makefile文件示例中,头文件被列为(并且仅列出)作为这样的目标的依赖项,没有特定的规则来定位头文件,就像cc文件在规则中的目标一样这个例子

sample1.o : sample1.cc sampleheader1.h
   g++ -Wall -c sample1.cc
Run Code Online (Sandbox Code Playgroud)

对于我的项目src/hello.cc headers/cctz.h,我有一个这样的目录结构

cctz.h作为一个头文件包括在由hello.cc.

但是,当我为我的项目创建一个makefile时,make抱怨所no rule to make target 'cctz.h需要的hello.o.makefile在我的src目录中,因此../headers达到了cctz.h

CPPOPTIONS = -I../headers
all: hello
hello: hello.o
  g++ -Wall -o $@
hello.o: hello.cc cctz.h
  g++ -Wall $(CPPOPTIONS) -c hello.cc
Run Code Online (Sandbox Code Playgroud)

因此,在我的程序中,我需要target头文件,但在我看到的其他makefile中(例如我提供了一个代码片段)标题只列为目标的依赖项,并且没有规则可以实现.我做错了什么或为什么我需要cctz.h在我的情况下定位文件(我该怎么做)

Som*_*ude 5

make程序会在makefile文件目录中的文件首先,除非你指定的文件的路径:

hello.o: hello.cc ../headers/cctz.h
Run Code Online (Sandbox Code Playgroud)

make对您在例如中设置的编译器选项一无所知CPPOPTIONS.

你也可以设置VPATH:

VPATH = ../headers
Run Code Online (Sandbox Code Playgroud)