Makefile vpath不适用于头文件

Nik*_*Nik 1 c c++ gcc makefile

我试图在我的Makefile中使用vpath,以避免为每个源文件添加目录名前缀.但我不能让它正常工作.

这是Makefile:

CC=gcc -Wall

vpath %.h include
vpath %.c src 

all: main.c Event.o Macros.h
        $(CC) $< Event.o -o test/a.out  

Event.o: Event.c Event.h Macros.h
        $(CC) -c $< -o $@
Run Code Online (Sandbox Code Playgroud)

SRC目录被正确包括在内.即,通过gcc找到Event.c文件.但是Event.h和Macros都没有.我在gcc中遇到错误,说在编译Event.c时找不到这两个文件.

我尝试一次将C文件中的#include指令更改为每个.

#include "Event.h" /* doesnt work */
#include <Event.h> /* doesnt work */
#include "../include/Event.h" /* works */
Run Code Online (Sandbox Code Playgroud)

你能帮帮我解决这个问题吗?我真的想避免在每个源文件之前使用目录名,因为我的实际Makefile比这大.

Oli*_*rth 9

vpath指令仅控制Make查找依赖项的方式; 它不会以任何方式影响GCC的工作方式.如果您在其他目录中有标题,则明确需要告诉GCC -I:

INCLUDE := include

$(CC) -I$(INCLUDE) $c $< -o $@
Run Code Online (Sandbox Code Playgroud)