我对makefile搜索include路径感到困惑。
假设我有一个文件结构:
.
├── hdrMainFolder.h
├── headers
│ └── hdrDifferentPath.h
├── makefile
├── sourceCode.cpp
└── src
├── hdrSamePath.h
└── src1.cpp
我在makefile中使用-I
选项来指示所包含标头的路径。
以下是src1.cpp 中包含的标题
#include "hdrSamePath.h"
#include "hdrMainFolder.h"
#include "hdrDifferentPath.h"
我应该在makefile中明确指出哪些路径?其中哪些是不必要的?下面这样够了吗?
INCLUDING = -Isrc -Iheaders
如果只有同一路径下的源文件包含标头,是否有必要指示标头的路径?
Which of the paths I should indicate explicitly in the makefile?
Which of them are unnecessary?
在命令shell上,运行编译指令的目录是编译器的当前目录
[用户使用直接命令或使用makefile进行编译]
当前目录(./)默认包含在编译器的头文件搜索路径中。
如果创建子目录并将头文件放置在子目录结构中,则需要为每个包含所需头文件的子目录显式添加-I
规则。
hdrMainFolder.h -> present in current directory, no need to add -I rule for this
hdrDifferentPath.h -> need to add -I rule (-I./headers)
hdrSamePath.h -> need to add -I rule (-I./src)
[你可以省略./在上面添加的-I
规则中,为了更清楚起见,我遵循]
Is it necessary to indicate to the path of a header, if it is only
included by a source file under the same path?
是的,源文件位置不用于确定用户定义的头文件搜索路径。需要明确提及。