Makefile pattern rule fails?

后端 未结 5 1025
春和景丽
春和景丽 2021-01-04 08:18

While using GNU-make, my Makefile has some pattern rule as:

%.o:%.c
    gcc $< -o:$@

This rule is added by me.

But when I do mak

相关标签:
5条回答
  • 2021-01-04 08:28

    Just ran through this problem.

    If this is your rule:

    %.o:%.c
        gcc $< -o:$@
    

    Make sure that you have a tab and not spaces on the second line before the gcc. Took me a couple of hours to figure out. Also no ':' after the -o flag, as somebody else pointed out.

    0 讨论(0)
  • 2021-01-04 08:34

    Are you sure the .c file exists? I think using -d as suggested earlier should help debug this.

    0 讨论(0)
  • 2021-01-04 08:38

    If you put only this in a Makefile and call make:

    %.o: %.cpp
      g++ -g -o $@ -c $<
    

    It tells: make: *** No targets. Stop.

    Because it's not a target. It's a rule.

    It will work if your another target needs a .o file.

    main.exe: main.o add.o
      g++ -g -o $@ $^
    
    %.o: %.cpp
      g++ -g -o $@ -c $<
    
    0 讨论(0)
  • 2021-01-04 08:40

    Make only uses a pattern rule as a sort of fallback. For example:

    $ ls
    Makefile
    $ cat Makefile
    %.o: %.c
        gcc $< -o $@
    $ make
    make: *** No targets.  Stop.
    

    This is fine. Make does not consider the pattern rule as you have not asked it to make anything, and there is no default target in the makefile. Let's ask it to make something that might use the pattern rule.

    $ make 1.o
    make: *** No rule to make target `1.o'.  Stop.
    

    Expected. 1.c does not exist, so the pattern rule is not considered. Let's try again.

    $ touch 1.c
    $ make 1.o
    gcc 1.c -o 1.o
    

    (and then some error about main being missing).

    Personally I dislike these fallback rules intensely. I much prefer listing the targets explicitly. Something like

    file.o file2.o f.o: %.o: %.c
        ...
    

    gives you a Target Specific Pattern Rule. These are quite different (see the manual). [Oh, and the pattern gives you no advantage in this noddy example.]

    0 讨论(0)
  • 2021-01-04 08:47

    You might need spaces around the : in the first line of your rule. Also, gcc does not take a colon before the output file name; just use -o $@.

    0 讨论(0)
提交回复
热议问题