0

what's wrong with my makefile?

CC = gcc OBJS = main.o map.o extended_map.o election.o utilities.o EXEC = election DEBUG_FLAG = -DNDEBUG COMP_FLAG = -std=c99 -Wall -pedantic-errors -Werror $(EXEC) : $(OBJS) $(CC) $(DEBUG_FLAG) $(OBJS) -o $@ main.o: main.c map.h election.h test_utilities.h $(CC) -c $(DEBUG_FLAG) $(COMP_FLAG) $*.c map.o: map.c map.h utilities.h $(CC) -c $(DEBUG_FLAG) $(COMP_FLAG) $*.c extended_map.o: extended_map.c extended_map.h map.h utilities.h $(CC) -c $(DEBUG_FLAG) $(COMP_FLAG) $*.c election.o: election.c election.h map.h extended_map.h utilities.h $(CC) -c $(DEBUG_FLAG) $(COMP_FLAG) $*.c utilities.o: utilities.c utilities.h $(CC) -c $(DEBUG_FLAG) $(COMP_FLAG) $*.c clean: rm -f $(OBJS) $(EXEC) 

I am running the following command on linux:

-bash-4.2$ ls election.c extended_map.c main.c makefile mtm_map utilities.c election.h extended_map.h main.o map.h test_utilities.h utilities.h -bash-4.2$ make make: *** No rule to make target `map.c', needed by `map.o'. Stop. -bash-4.2$ 

map.c exists inside a folder named mtm_map inside the current folder

4
  • 2
    The makefile expects map.c to be in the current folder, not a subdirectory. Commented May 8, 2020 at 21:04
  • 8
    I've rolled back your edit. It is inappropriate here to deface your question after it's been posted according to the Terms of Service. Please do not do so again. Commented May 9, 2020 at 0:25
  • 6
    Please don't make more work for others by vandalizing your posts. By posting on the Stack Exchange (SE) network, you've granted a non-revocable right, under a CC BY-SA license, for SE to distribute the content (i.e. regardless of your future choices). By SE policy, the non-vandalized version is distributed. Thus, any vandalism will be reverted. Please see: How does deleting work? …. If permitted to delete, there's a "delete" button below the post, on the left, but it's only in browsers, not the mobile app. Commented May 9, 2020 at 22:11
  • mod message: post locked. Please don't rollback anymore after the lock period. Commented May 10, 2020 at 9:15

1 Answer 1

1

You told make how to build "map.o" out of "map.c", but you don't have "map.c". You have "mtm_map/map.c".

You probably want something like

CC = gcc OBJS = main.o mtm_map/map.o extended_map.o election.o utilities.o EXEC = election DEBUG_FLAG = -DNDEBUG COMP_FLAG = -std=c99 -Wall -pedantic-errors -Werror $(EXEC) : $(OBJS) $(CC) $(DEBUG_FLAG) $(OBJS) -o $@ main.o: main.c map.h election.h test_utilities.h $(CC) -c $(DEBUG_FLAG) $(COMP_FLAG) $*.c mtm_map/map.o: mtm_map/map.c map.h utilities.h $(CC) -c $(DEBUG_FLAG) $(COMP_FLAG) $*.c extended_map.o: extended_map.c extended_map.h map.h utilities.h $(CC) -c $(DEBUG_FLAG) $(COMP_FLAG) $*.c election.o: election.c election.h map.h extended_map.h utilities.h $(CC) -c $(DEBUG_FLAG) $(COMP_FLAG) $*.c utilities.o: utilities.c utilities.h $(CC) -c $(DEBUG_FLAG) $(COMP_FLAG) $*.c clean: rm -f $(OBJS) $(EXEC) 
Sign up to request clarification or add additional context in comments.

9 Comments

I don't know what you mean by changing $*.c but there's no way to fix your problem by changing the content of the recipe. Make doesn't care about the content of the recipe; it simply hands it off to the shell. In order to fix your problem you'll have to change the target and prerequisites, as shown here.
@Chad Miller provided you with a solution to the question you asked, and that solution worked for that question. Now you've asked a different question, that is not related to the first one. SO is not intended to be a "running tutorial session". It's intended that you first try to solve your problem, then if you can't you post a question for that specific problem describing what you have tried and what didn't work. In your comment above for example you say "a new error" but you don't tell us what source file failed to compile.
Most likely you need to add -I$(CURDIR) to your compile line (the COMP_FLAG variable) so that the compiler can find your header files.
His code DID work, in that the problem you had before No rule to make target is now gone. Problem solved. Now you have a new and different problem, which is your compile fails because it can't find a header file. There's no way we can foresee this new problem because it depends on the content of all your source files and what they include, and how. You need to use the -I compiler flag once for each directory that contains your header files that you want to include. If they're all in one directory, you just need it once with that directory.
The compiler is not going to go searching through your entire system (or even the subdirectories of the current directory) trying to find files. You have to tell it where they are.
|