4

I'm following this guide about makefile, but I didn't understand at all the last example and I can't get my makefile work, as I obtain the error make: *** No rule to make target "obj/date.o", needed by "Whatsapp". Stop.

Here is my makefile:

IDIR = ../include
CC = gcc
CFLAGS = -I$(IDIR)

ODIR = obj
LDIR = ../lib

LIBS = -lncurses

# Keep the alphabetical order!
_DEPS = \
constants.h\
date.h\
inOut.h\
languages.h\
message.h\
mycurses.h\
mysocket.h\
text.h\
time.h\
user.h\

DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))

# Keep the alphabetical order!
_OBJ = \
date.o\
inOut.o\
languages.o\
main.o\
message.o\
mycurses.o\
mysocket.o\
text.o\
time.o\
user.o\

OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))

# these two lines should tell the compilator that my .o files depend by .c files, don't they?
$(ODIR)/%.o: %.c $(DEPS)
    $(CC) -c -o $@ $< $(CFLAGS)

Whatsapp:   $(OBJ)
    $(CC) -o $@ $^ $(CFLAGS) $(LIBS)

.PHONY: clean

clean:
    rm -f $(ODIR)/*.o *~ core $(INCDIR)/*~ 

Obviously, all my *.c are in the current folder, so really I don't know what I am missing.

Just for more clarity, here is the content of the current folder:

urbijr@urbijr-VirtualBox:/media/sf_Whatsapp_CLIENT$ ls
constants.h  indexbook.txt  languages.c  makefile              message.h    mycurses.h  text.c  time.h
date.c       inOut.c        languages.h  message.c             message.txt  mysocket.c  text.h  user.c
date.h       inOut.h        main.c       message_for_user.txt  mycurses.c   mysocket.h  time.c  user.h
UrbiJr
  • 133
  • 1
  • 2
  • 20
  • 1
    Are you sure that `data.c` is present and the name is exactly as I wrote? – LPs Feb 03 '17 at 15:00
  • try `make -f Makefile` and tell us the output. – LPs Feb 03 '17 at 15:02
  • I tried and it's exactly the same output – UrbiJr Feb 03 '17 at 15:03
  • Look at what's in `$(OBJ)` and `$(_OBJ)`. – melpomene Feb 03 '17 at 15:15
  • @melpomene what you mean? – UrbiJr Feb 03 '17 at 15:15
  • Check the contents of those variables. What's unclear? – melpomene Feb 03 '17 at 15:16
  • is your Makefile *exactly* how you've pasted it above? Including the lack of whitespace before each object file in _OBJ? – Chris Turner Feb 03 '17 at 15:18
  • yes, without whitespace. Where should i add it? – UrbiJr Feb 03 '17 at 15:20
  • 1
    As it turns out, [*Outside of recipe lines, backslash/newlines are converted into a single space character*](https://www.gnu.org/software/make/manual/html_node/Splitting-Lines.html#Splitting-Lines), so the variables actually contain spaces. – melpomene Feb 03 '17 at 15:23
  • no, it's fine - it just looked like it'd been pasted weirdly. – Chris Turner Feb 03 '17 at 15:24
  • this line: `rm -f $(ODIR)/*.o *~ core $(INCDIR)/*~` is referencing the macro `INCDIR`, BUT that macro is never defined. – user3629249 Feb 04 '17 at 17:47
  • this line: `$(CC) -o $@ $^ $(CFLAGS) $(LIBS)` is trying to perform the link operation. When linking, the `-I...` operator is not used, What should be used, inplace of the `$(CFLAGS)` is `-L$(LDIR)` – user3629249 Feb 04 '17 at 17:54
  • rather than itemizing the list of object files and list of header files Suggest: `SRC := $(wildcard:*.c)` `OBJ := $(SRC:.%.c=$(ODIR)/%.o) ` `DEPS := $(SRC:%.c= $(IDIR)/%.h)` There is also nothing to actually copy the header files to the `$(IDIR)` – user3629249 Feb 04 '17 at 17:59
  • this line: `$(CC) -c -o $@ $< $(CFLAGS)` will perform the compiles with the default warnings enabled (most likely none of the warnings) Suggest: `$(CC) -Wall -Wextra -pedantic -Wconversion -std=gnu99 -ggdb -c -o $@ $< $(CFLAGS)` – user3629249 Feb 04 '17 at 18:06
  • to be able to run the makefile with minimal fuss, (because make will fixate on the first target listed, which is the compile recipe, strongly suggest the recipe starting with `Whatsapp` be listed before the compile recipe. – user3629249 Feb 04 '17 at 18:09

1 Answers1

3

Your dependencies are not as you have specified in your makefile. The line below specifies that all include files should be in the directory $(IDIR).

DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))

This is set as ../include, but it seems you have all the header files in the same directory. Either move them to ../include or change IDIR to . (the current directory).

You'll also need to create the output directory (obj) as make won't do this automatically.

Michael Mior
  • 28,107
  • 9
  • 89
  • 113
  • @JohnBollinger When fixing this error, the program builds fine with me (with dummy code using the same names given). – Michael Mior Feb 03 '17 at 15:32
  • Yes. I get the same error before my change and after my change, the problem is resolved. – Michael Mior Feb 03 '17 at 15:55
  • thanks. the problem was that as I said I didn't understand correctly all the code of the last makefile example of that guide and all I had to do was just create the directories – UrbiJr Feb 03 '17 at 17:40