【问题标题】:How can I include headers from a directory and all its subdirectories in my makefile?如何在我的 makefile 中包含目录及其所有子目录的标题?
【发布时间】:2019-08-09 00:46:22
【问题描述】:

我的项目组织如下:

  • 生成文件
  • 构建
    • 对象
    • 应用程序
  • 包括
    • 子目录(包含 *.h)
    • 子目录(包含 *.h)
    • 子目录(包含 *.cpp)
    • 子目录(包含 *.cpp)

在我的 makefile 中,从我当前使用的子目录中获取所有 *.h:

#includes (.h / .hpp)
INC_DIRS := \
$(wildcard include/*/)

INCLUDE = $(foreach d, $(INC_DIRS), -I$d)

确实工作,我可以编译得很好。但是我想知道是否有一种更简单的方法可以将所有子目录包含在单个包含语句中。

我当前使用的方法为每个子目录执行一个“-Iinclude/subdirectory”,它在终端中看起来非常混乱和混乱。

【问题讨论】:

  • 不要那样做!你应该只做 -Isrc -Iinclude 并且你的源代码应该做 #include for include/foo/bar.h

标签: c++ linux makefile


【解决方案1】:

鉴于您的包含路径方法,这是在您的 makefile 中指定它的好方法。

但是,最好要求所有这些子目录都在路径中,而是在代码中使用相对路径。

#include "somedir/thing.h"
#include "somedir2/otherthing.h"

等等。

如果需要,这也将更容易移植到其他构建系统。

【讨论】:

  • 否则 #include for include/foo/bar.h
猜你喜欢
  • 2021-06-19
  • 2021-01-16
  • 2014-04-28
  • 2014-03-08
  • 2021-01-05
  • 1970-01-01
  • 1970-01-01
  • 2017-03-15
  • 1970-01-01
相关资源
最近更新 更多