我试图使用GCC (linux)与makefile编译我的项目。

我得到了以下错误,似乎无法在这种情况下破译:

"No rule to make target 'vertex.cpp', needed by 'vertex.o'.  Stop."

这是生成文件:

a.out: vertex.o edge.o elist.o main.o vlist.o enode.o vnode.o
    g++ vertex.o edge.o elist.o main.o vlist.o enode.o vnode.o

main.o: main.cpp main.h
    g++ -c main.cpp

vertex.o: vertex.cpp vertex.h
    g++ -c vertex.cpp

edge.o: edge.cpp edge.h
    g++ -c num.cpp

vlist.o: vlist.cpp vlist.h
    g++ -c vlist.cpp

elist.o: elist.cpp elist.h
    g++ -c elist.cpp

vnode.o: vnode.cpp vnode.h
    g++ -c vnode.cpp

enode.o: enode.cpp enode.h
    g++ -c node.cpp

当前回答

我的箱子里少了一块钱:

而不是:

(LINK_TARGET): $(OBJS)

应该是:

$(LINK_TARGET): $(OBJS)

编辑:

我也遇到了同样的问题,但现在由于另一个原因,这是由于我的.bashrc文件中的一个echo命令。

其他回答

在我的例子中,这是由于我调用Makefile: Makefile(全部大写)

一个常见的错误可能是错别字在另一个文件的名称。

你的例子很简单,但有时可能会混淆 make本身的信息。让我们考虑一个例子。

我的文件夹内容是:

$ ls -1
another_file
index.md
makefile

而我的makefile是这样的

all: index.html

%.html: %.md wrong_path_to_another_file
    @echo $@ $<

尽管我有索引。Md它应该在哪里,没有错误的名字,从make的消息将是

make: *** No rule to make target `index.html', needed by `all'.  Stop.

说实话,这个信息令人困惑。它只是说,没有规则。实际上,这意味着规则是错误的,但由于通配符(模式)规则使无法确定究竟是什么导致了问题。

让我们稍微修改一下makefile,也就是说用显式规则替换模式:

index.html: index.md wrong_path_to_another_file

现在我们得到的信息是:

make: *** No rule to make target `wrong_path_to_another_file', needed by `index.html'.  Stop.

奇迹!可以得出以下结论:

make消息依赖于规则,并不总是指向问题的根源 makefile中可能存在与此消息所指定的不同的其他问题

现在我们提出了检查规则中其他依赖项的想法:

all: index.html

%.html: %.md another_file
    @echo $@ $<

只有这样,我们才能得到想要的结果:

$ make
index.html index.md

造成这个错误的原因有很多。

我遇到这个错误的原因之一是在为linux和windows构建时。

我有一个大写的文件名BaseClass.h SubClass.h Unix维护有区分大小写的文件名约定,而windows是不区分大小写的。

c++中为什么人们不用大写的头文件名?

如果您正在使用gmake,请尝试使用gmake clean编译干净的构建

一些文本编辑器具有忽略区分大小写的文件名的默认设置。这也可能导致同样的错误。

如何在Qt Creator中添加一个名字以大写字母开头的c++文件?它会自动把它变成小字母

我发现的问题比其他人提到的更愚蠢。

我们的makefile会得到要构建的东西的列表。有人将TheOtherLibrary添加到其中一个列表,如下所示。

LIBRARYDIRS = src/Library
LIBRARYDIRS = src/TheOtherLibrary

他们应该这样做:

LIBRARYDIRS = src/Library
LIBRARYDIRS += src/TheOtherLibrary

如果他们采用第二种方法,他们就不会摧毁图书馆了。+=中的加号非常重要。

在我的例子中,我愚蠢地使用逗号作为分隔符。用你的例子来说,我是这样做的:

a.out: vertex.o, edge.o, elist.o, main.o, vlist.o, enode.o, vnode.o
    g++ vertex.o edge.o elist.o main.o vlist.o enode.o vnode.o

把它换成

a.out: vertex.o edge.o elist.o main.o vlist.o enode.o vnode.o
    g++ vertex.o edge.o elist.o main.o vlist.o enode.o vnode.o

固定它。