首页 > 解决方案 > 如何让 makefile 正确识别多个目录中的包含

问题描述

由于编译时出现包含错误,我一直在努力让我的 make 文件正确编译我的所有源文件。我在网上查看了一些问题,但解决方案似乎无法解决我的问题。

如果我将所有源文件都放在同一个目录中,我可以编译这个程序,但是一旦我尝试将文件拆分到单独的目录中,它就不再起作用了。

这是错误:

~/Documents/GitHub/testing$ make build
gcc -I/. -Wall main.c inplementation/inplementation.c -o program.exe

In file included from main.c:3:
./inplementation/inplementation.h:4:10: fatal error: 'module/module.h' file not found
\#include "module/module.h"

1 error generated.

In file included from inplementation/inplementation.c:1:
inplementation/inplementation.h:4:10: fatal error: 'module/module.h' file not found
\#include "module/module.h"
1 error generated.
make: *** [build] Error 1

这是我的文件目录的屏幕截图:

文件目录

这是我这个项目的包含路径的屏幕截图: 包括路径

这是我的制作文件的屏幕截图(特别是构建): makefile 中的构建函数

标签: cmakefileincludeinclude-path

解决方案


您使用#include "module/module.h"while 包含标志是-I/$(TESTING_DIR)/module.

首先,我认为您可能的意思是-I$(TESTING_DIR)/module,否则您将在存储的路径之前添加一个额外的斜杠TESTING_DIR

但主要问题是该文件夹module/module.h下不存在。module因此,您需要执行以下组合之一:

  • -I$(TESTING_DIR)/module#include "module.h"
  • -I$(TESTING_DIR)#include "module/module.h"

推荐阅读