0

I want to make a simple makefile for a C project that have the following directories.

 -Project  
   - src 
     - a.c  
     - b.c
     - main.c  
   - headers  
     - a.h  
     - b.h   
   - build  
     - makefile  
     - project.exe 

And here it's the makefile that I've done.

project: a.o b.o main.o
    cc -o sesion0 a.o b.o main.o
a.o: ../src/a.c ../headers/a.h 
b.o: ../src/b.c ../headers/b.h 
main.o: ../src/main.c ../headers/a.h ../headers/b.h 

But when I execute the make order, it tell's me that the file or directory a.o, b.o and main.o doesn't exist and also that there's not input files. In the end shows this error:

make: *** [project] Error 1

Does anyone know why this happen or where I have the error? I don't know very well how to manage the directories in the makefile.

Thanks.

Peter
  • 1
  • 1

1 Answers1

2

Make has built-in rules for making x.o from x.c, but not from ../src/x.c. In other words, paths of input and output must be the same, only the file extension differs.

You can fix it by using VPATH for directory search:

VPATH = ../src:../headers
a.o: a.c a.h 
b.o: b.c b.h 
main.o: main.c a.h b.h 
Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
  • Thanks for the anwser Maxim but know it shows me another error. This is the error: `cc -c -o a.o ../src/a.c ../src/a.c:1:21: error: a.h: Don't exist the file o directory make: *** [a.o] Error 1` Do you know why this happen? It seems that it doesn't look for the a.h file in the ../headers directory. – Peter Feb 08 '11 at 15:43
  • Now you need to add compiler include paths. Add `CPPFLAGS=-I../headers` somewhere in your Makefile. – Maxim Egorushkin Feb 08 '11 at 15:48
  • @MaximEgorushkin the following question has the same set of issues when dealing with multiple targets and their dependencies? https://stackoverflow.com/questions/30043480/make-recipe-to-prevent-rebuilding-of-non-dependent-targets# – Sami Kenjat May 05 '15 at 05:34