make: 2 dependencies expected but only 1 exists

时间:2016-10-15 17:23:20

标签: makefile gnu-make

Lets assume I have a little project with the following make-file. Please attention to the generation of the o-file (2 dependencies - to the .cpp and the .hpp).

LIB_OPTS = -lpthread
CPP_OPTS = -std=c++11 -Wall

all: main
   ${CXX} ${CPP_OPTS} ${LIB_OPTS} *.o -o main

main: a.o b.o c.o main.o

%.o: %.cpp %.hpp
    ${CXX} ${CPP_OPTS} ${LIB_OPTS} -g -c $< -o $@

clean:
   rm *.o

a, b and c do have cpp and hpp-files.

main does only have a .cpp.

Here is the output.

g++ -std=c++11 -Wall -lpthread -g -c a.cpp -o a.o
g++ -std=c++11 -Wall -lpthread -g -c b.cpp -o b.o
g++ -std=c++11 -Wall -lpthread -g -c c.cpp -o c.o
g++    -c -o main.o main.cpp
g++ -std=c++11 -Wall -lpthread *.o -o main

What about the generation of main.o???

Where does that come from? Thats nothing to do with my rule.

If I have a main.hpp it would catch my rule. Sure I can create a separate rule for main.o but I expected %.o to make it.

Thanks for help! Chris

1 个答案:

答案 0 :(得分:2)

GNU make有许多内置规则。其中包含一条知道如何从.o文件创建.cpp文件的规则。由于您的所有规则都无法创建main.o,因此请查看其自己的内置规则并找到可以使用的规则。

您可以使用以下命令查看内置规则的完整列表:

make -pf/dev/null

您可以运行make,以便它不会将任何自己的内置规则与make -r一起使用。