简体   繁体   English

第一个 Makefile 问题:没有制定目标的规则

[英]First Makefile Questions: No rule to make target

I'm attempting to create my first ever Makefile and looking for the most simple, understandable version--even if it isn't "good practice."我正在尝试创建我的第一个 Makefile 并寻找最简单易懂的版本——即使这不是“好的做法”。 I'll deal with that at a later time.稍后我会处理这个问题。 :) :)

Basically, I just want to run make in the command line and have it execute the several g++ commands so I don't have to.基本上,我只想在命令行中运行make并让它执行几个 g++ 命令,这样我就不必了。 For example, instead of entering the following (at top level dir) in the command line...例如,不要在命令行中输入以下内容(在顶级目录中)...

$g++ -c ./src/RaspPi4Main.cpp -o ./src/RaspPi4Main.o

$g++ -c ./src/funcI2cCom/funcI2cCom.cpp -o ./src/funcI2cCom/funcI2cCom.o

$g++ -o ./bin/RaspPi4Main ./src/RaspPi4Main.o ./src/funcI2cCom/funcI2cCom.o -lwiringPi

...I'd like something similar to my attempt at a Makefile: ...我想要类似于我在 Makefile 上的尝试:

RaspPi4Main: RaspPi4Main.o funcI2cCom.o (and all header files? or not needed if added below?)
    g++ -o ./bin/RaspPi4Main ./src/RaspPi4Main.o  ./src/funcI2cCom/funcI2cCom.o -lwiringPi

RaspPi4Main.o: RaspPi4Main.cpp
    g++ -c ./src/RaspPi4Main.cpp -o ./src/RaspPi4Main.o

funcI2cCom.o: funcI2cCom.cpp funcI2cCom.h
    g++ -c ./src/funcI2cCom/funcI2cCom.cpp -o ./src/funcI2cCom/funcI2cCom.o

Hopefully, I'm close?希望,我很接近? Thanks!谢谢!

As a first step, you need to make sure your recipes produce their target and that inputs are either files or targets of other rules:作为第一步,您需要确保您的配方产生它们的目标,并且输入是文件或其他规则的目标:

bin/RaspPi4Main: src/RaspPi4Main.o src/funcI2cCom/funcI2cCom.o 
    g++ -o bin/RaspPi4Main src/RaspPi4Main.o src/funcI2cCom/funcI2cCom.o -lwiringPi

src/RaspPi4Main.o: src/RaspPi4Main.cpp
    g++ -c src/RaspPi4Main.cpp -o src/RaspPi4Main.o

src/funcI2cCom/funcI2cCom.o: src/funcI2cCom/funcI2cCom.cpp src/funcI2cCom/funcI2cCom.h
    g++ -c src/funcI2cCom/funcI2cCom.cpp -o src/funcI2cCom/funcI2cCom.o

Note that this is quite verbose.请注意,这是非常冗长的。 You can shorten it quite significantly by making use of automatic variables .您可以通过使用自动变量显着缩短它。 Concretely, $@ is the name of the target, $< is the name of the first dependency, and $^ are all dependencies:具体来说, $@是目标的名字, $<是第一个依赖的名字, $^是所有的依赖:

bin/RaspPi4Main: src/RaspPi4Main.o src/funcI2cCom/funcI2cCom.o 
    g++ -o $@ $^ -lwiringPi

src/RaspPi4Main.o: src/RaspPi4Main.cpp
    g++ -c $< -o $@

src/funcI2cCom/funcI2cCom.o: src/funcI2cCom/funcI2cCom.cpp src/funcI2cCom/funcI2cCom.h
    g++ -c $< -o $@

Note that there's quite some repetition.请注意,有相当多的重复。 Luckily, Make already knows how to compile a .cpp file into a .o file through implicit rules .幸运的是,Make 已经知道如何通过隐式规则将 .cpp 文件编译为 .o 文件。 Thus you can omit the recipes for these rules:因此,您可以省略这些规则的配方:

bin/RaspPi4Main: src/RaspPi4Main.o src/funcI2cCom/funcI2cCom.o 
    g++ -o $@ $^ -lwiringPi

src/RaspPi4Main.o: src/RaspPi4Main.cpp
src/funcI2cCom/funcI2cCom.o: src/funcI2cCom/funcI2cCom.cpp src/funcI2cCom/funcI2cCom.h

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

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