简体   繁体   English

makefile找不到.o文件

[英]makefile can't find .o file

I'm working on a simple makefile using implicit rules, and not really understanding how make is deciding its execution order. 我正在使用隐式规则来处理简单的makefile,但并没有真正理解make如何确定其执行顺序。 When I have my makefile setup like this, it says it can't find Wave.o: 当我这样设置makefile时,它说找不到Wave.o:

CXX = g++

AudioSample: Wave.o AudioSample.o 
    $(CXX) -o AudioSample AudioSample.o Wave.o

AudioSample.o: Wave.h PCADPCM.h
    $(CXX) -c AudioSample.cpp

Wave.o: Wave.h

Terminal output: 终端输出:

g++ -o AudioSample AudioSample.o Wave.o
g++: error: Wave.o: No such file or directory
make: *** [AudioSample] Error 1

However when I change the order of the targets it works fine: 但是,当我更改目标的顺序时,它可以正常工作:

AudioSample: Wave.o AudioSample.o 
    $(CXX) -o AudioSample AudioSample.o Wave.o

Wave.o: Wave.h

AudioSample.o: Wave.h PCADPCM.h
    $(CXX) -c AudioSample.cpp

Terminal output: 终端输出:

g++    -c -o Wave.o Wave.cpp
g++ -c AudioSample.cpp
g++ -o AudioSample AudioSample.o Wave.o

Whats the problem?? 有什么问题??

Double-check for Tabs vs Spaces in your Makefile . 仔细检查Makefile Tabs vs Spaces。

For example with Vim editor, you can search for tabs /\\t or add the following to your config: 例如,使用Vim编辑器,您可以搜索选项卡/\\t或将以下内容添加到配置中:

" in ~/.vimrc
listchars=tab:»»,trail:·,nbsp:~
set list

You clearly have a hidden Tab in the Wave.o rule (shown like in my editor): Wave.o规则中,您显然具有一个隐藏的Tab (如在我的编辑器中所示):

Wave.o : Wave.h
»»»»»»

This tab breaks implicit rules to generate Wave.o . 选项卡打破了隐式规则以生成Wave.o Remove extra Tabs and it will be fine like your second Makefile where there is no room for Tab as the target is the very last line of file. 删除多余的Tab,就像第二个Makefile ,其中没有Tab的空间,因为目标是文件的最后一行。

For information, Wave.h is a prerequisite and is not used by make to guess the source file name ( Wave.cpp in this case). 有关信息, Wave.h先决条件make不会使用它来猜测源文件名(在这种情况下为Wave.cpp )。 Even Wave.o : Wave.cpp wouldn't have helped. 甚至Wave.o : Wave.cppWave.o : Wave.cpp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM