简体   繁体   English

C ++ Makefile未创建必要的.o文件

[英]C++ makefile not creating a necessary .o file

The following is my makefile for compiling a few basic C++ files into an executable: 以下是我的makefile,用于将一些基本的C ++文件编译为可执行文件:

OBJ= node.o link.o trie.o testtrie.o
OPTS= -g -c -Wall -Werror

testtrie: $(OBJ)
        g++ -o testtrie $(OBJ)

testtrie.o: prog3.cc trie.h trie.cc link.h link.cc node.h node.cc
        g++ $(OPTS) prog3.cc

trie.o: trie.cc trie.h
        g++ $(OPTS) trie.cc

link.o: link.cc link.h
        g++ $(OPTS) link.cc

node.o: node.cc node.h
        g++ $(OPTS) node.cc

clean:
        rm -f *.o *~

However, when I run make , upon trying to compile prog3.cc, it says: 但是,当我运行make ,在尝试编译prog3.cc时,它说:

g++: testtrie.o: No such file or directory. g ++:testtrie.o:没有这样的文件或目录。

I cannot for the life of me figure out why it doesn't make testtrie.o... 我无法为自己的一生弄清楚为什么它不能使testtrie.o ...

Thanks in advance for your help! 在此先感谢您的帮助!

Your testtrie.o rule: 您的testtrie.o规则:

testtrie.o: prog3.cc trie.h trie.cc link.h link.cc node.h node.cc
    g++ $(OPTS) prog3.cc

Does not actually build testtrie.o , it builds prog3.o . 实际上并不构建testtrie.o ,而是构建prog3.o

(It also has several prerequisite source files which it doesn't actually use, unless you're doing some unhealthy things with, eg, #include .) (它也有一些前提条件源文件,它们实际上并没有使用,除非您使用#include做一些不健康的事情。)

That is because your executable ( testtrie ) depends on testtrie.o . 那是因为您的可执行文件( testtrie )依赖于testtrie.o

By default, a compiler (g++ in our case), when compiling a source file (eg src.cc ) if -o is not given the output object file will match the source file name, so it will be src.o . 默认情况下,如果未指定-o,则编译器(在本例中为g ++)在编译源文件(例如src.cc )时,输出对象文件将与源文件名匹配,因此将为src.o。 Your testtrie.o target is never generated; 您的testtrie.o目标永远不会生成; instead prog3.o is: 而是prog3.o是:

testtrie.o: prog3.cc trie.h trie.cc link.h link.cc node.h node.cc
    g++ $(OPTS) prog3.cc

Modify the command to: 将该命令修改为:

    g++ $(OPTS) -o $@ prog3.cc

The rule for the target testtrie.o should build a testtrie.o file like this: 目标testtrie.o的规则应构建如下的testtrie.o文件:

testtrie.o: prog3.cc trie.h trie.cc link.h link.cc node.h node.cc
    g++ $(OPTS) prog3.cc -o testtrie.o

You can also add an automatic variable $@ to make it recognize the output filename based on the target name: 您还可以添加自动变量 $@以使其根据目标名称识别输出文件名:

testtrie.o: prog3.cc trie.h trie.cc link.h link.cc node.h node.cc
    g++ $(OPTS) prog3.cc -o $@

Both version will have the same effect. 这两个版本将具有相同的效果。 Also, remember to correct the remaining targets in the same way . 另外, 请记住以相同的方式校正其余目标

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

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