2

I am trying to use vpath in my Makefile to avoid prefixing every source file with directory name. But I can't get it to work properly.

Here's the 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 $@

The src directory is being included correctly. i.e Event.c file is found by gcc. But both Event.h and Macros .h are not. I get an errors in gcc saying that both files were not found when compiling Event.c.

I tried changing the #include directive in my C file to each of these at a time.

#include "Event.h" /* doesnt work */
#include <Event.h> /* doesnt work */
#include "../include/Event.h" /* works */

Can you please help me with this problem ? I really want to avoid using directory names before every source file as my actual Makefile is bigger than this.

Nik
  • 293
  • 5
  • 14

1 Answers1

10

The vpath directive only controls how Make finds dependencies; it doesn't affect in any way how GCC works. If you have headers in some other directory, you explicitly need to tell GCC with -I:

INCLUDE := include

$(CC) -I$(INCLUDE) $c $< -o $@
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • Is there a way to see the resolved path GCC created based on the includes in the project? `-M -MG` does what I want, Namely ignores errors, yet doesn't show the resolved paths. – Royi Oct 11 '17 at 22:45