【发布时间】:2017-12-21 22:53:51
【问题描述】:
我有一个小型构建,我只想包含来自不同目录的单个头文件(和源文件)。我知道如何使用 -I 参数,但不想包含整个目录,只包含单个文件。
我的 makefile 如下所示:
myproj: ui.o data.o string.o
c99 -ggdb -o myproj ui.o data.o string.o
ctags *
ui.o : ui.c data.h
c99 -ggdb -c ui.c
data.o : data.c data.h
c99 -ggdb -c data.c
string.o : ../../shared/src/string.c ../../shared/src/string.h
c99 -ggdb -c ../../shared/src/string.c ../../shared/src/string.h -o ./jcstring.o
当我试图让它抱怨找不到string.h时:
c99 -ggdb -c ui.c ../../shared/src/string.h
In file included from ui.c:7:0:
data.h:1:22: fatal error: string.h: No such file or directory
#include "string.h"
如何在不包含整个目录的情况下包含此文件?
【问题讨论】:
-
在“make”文件的早期生成源文件列表,可能通过 ``SRC := $( wildcard *.c )` 和目标文件列表,可能通过
OBJ := $(SRC:.c=.o) -
更改默认头文件搜索路径(环境中的一个条目)以包含所需文件的目录路径。另请注意,您的“make”文件会更好,因为它包含为每个源文件生成“依赖”文件的必要语句然后生成string.o的配方(最好不要在中途更改根名称)看起来类似于: %.o : %.c %d 紧跟在下一行类似于:
c99 -ggdb -c $ifneq "$(MAKECMDGOALS)" "clean" -include $(DEP) endif -
在“make”文件的开头包含类似于:
CCFLAGS := -Wall -Wextra -Wconversion -pedantic -std=gnu99的语句 -
注意:
$(DEP)宏的生成类似于:DEP := $(SRC:.c=.d)