1

i have little problem with making a make file, this is the code :

SHELL = /bin/sh

CC := gcc
CFLAGS := -Wall
VPATH = src:obj

HEADERS := parser.h
dirs = out obj

%.o : mkdirs %.c $(HEADERS)
    $(CC) -c $(word 2,$^) -o obj/$@ 

all : parser.o
    ar cr out/libsip.a $<  

clean : 
    rm -f -r $(dirs)

mkdirs :
    mkdir -p $(dirs)

when i try to execute make i get this error :

mkdir -p out obj
gcc -c src/parser.c -o obj/parser.o 
ar cr out/libsip.a parser.o
file parser.o not found

i don't understand why parser.o don't get replaced by the right path, i used automatic variable

KarimS
  • 3,812
  • 9
  • 41
  • 64

1 Answers1

1
%.o : mkdirs %.c $(HEADERS)
    $(CC) -c $(word 2,$^) -o obj/$@ 

all : parser.o
    ar cr out/libsip.a $<

You are telling make to create a parser.o file in the current directory.

Your %.o rule then creates the file in the obj directory not the current directory.

The ar command then can't find it.

VPATH is for finding prerequisites. If obj/parser.o already existed then make would find it for the all target I believe.

See How Not to Use VPATH.

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
  • the file obj/parser.o exist but parser.o don't get replaced by the real path – KarimS Jan 23 '15 at 18:34
  • I meant exists when you start make. And yes, it doesn't because that's not the prerequisite make tried to build. It tried to build `parser.o` you just happened to create `obj/parser.o` but make doesn't know that. – Etan Reisner Jan 23 '15 at 18:38
  • ah ok i think i understand, but can you propose a solution to me? – KarimS Jan 23 '15 at 19:39
  • @karim Yes, read the link I included. (Essentially don't use `VPATH` for this it doesn't work the way you want.) – Etan Reisner Jan 23 '15 at 20:30