简体   繁体   English

Makefile 未对文件使用隐式规则

[英]Makefile doesn't use implicit rule on file

I just wanted to write a quick Makefile for a small C++ project, but when I try to build, make says:我只是想为一个小型 C++ 项目写一个快速的 Makefile,但是当我尝试构建时,make 说:

No rule to make target "obj/main.o"没有规则使目标“obj/main.o”

even though my implicit rule should cover that file.即使我的隐含规则应该涵盖该文件。 I have seen others who had forgotten a slash or something, but I dont't see the problem with this Makefile:我见过其他人忘记了斜线什么的,但我没有看到这个 Makefile 的问题:

.PHONY: run, clean

SRCDIR:=./src
OBJDIR:=./obj
BINDIR:=./bin

CC:=gcc
CXX:=g++

SRC:=$(shell find -name *.cpp)
HXX:=$(shell find -name *.hpp)
OBJ:=$(subst src/,obj/,$(SRC:%.cpp=%.o))
EXE:=main

CXXFLAGS:=-std=c++14 -Wall

$(EXE): $(OBJ)
    @echo $(OBJ)
    $(CXX) $(CXXFLAGS) $^ -o $(BINDIR)/$@

$(OBJDIR)/%.o: $(SRCDIR)/%.cpp $(SRCDIR)/%.hpp
    $(CXX) $(CXXFLAGS) -c $< -o $@

I am using Raspberry Pi OS on my Raspberry Pi 3 b.我在 Raspberry Pi 3 b 上使用 Raspberry Pi OS。

because of因为

$(OBJDIR)/%.o: $(SRCDIR)/%.cpp $(SRCDIR)/%.hpp

you can only compile file having both a cpp and a hpp , but your main does not have header (eg you have a src/main.cpp but no src/main.hpp )您只能编译同时具有cpphpp的文件,但您的 main 没有 header (例如,您有src/main.cpp但没有src/main.hpp

Example:例子:

pi@raspberrypi:/tmp/p $ find .
.
./bin
./src
./src/main.cpp
./Makefile
./obj
pi@raspberrypi:/tmp/p $ make
make: ***  Aucune règle pour fabriquer la cible « obj/main.o », nécessaire pour « main ». Arrêt.
pi@raspberrypi:/tmp/p $ touch src/main.hpp
pi@raspberrypi:/tmp/p $ make
g++ -std=c++14 -Wall -c src/main.cpp -o obj/main.o
./obj/main.o
g++ -std=c++14 -Wall obj/main.o -o ./bin/main
pi@raspberrypi:/tmp/p $ 

( Aucune règle pour fabriquer la cible... means No rule to make target ) Aucune règle pour fabriquer la cible...表示没有制定目标的规则


Changing the line by换行

$(OBJDIR)/%.o: $(SRCDIR)/%.cpp

the compilation is done:编译完成:

pi@raspberrypi:/tmp/p $ find .
.generally
./bin
./src
./src/main.cpp
./Makefile
./obj
pi@raspberrypi:/tmp/p $ make
g++ -std=c++14 -Wall -c src/main.cpp -o obj/main.o
./obj/main.o
g++ -std=c++14 -Wall obj/main.o -o ./bin/main
pi@raspberrypi:/tmp/p $ 

To add the dependency to the header is not enough to recompile when necessary when a source #include other header files which is often the case.当源#include其他 header 文件经常出现时,将依赖项添加到 header 不足以在必要时重新编译。

You can look at makedepend , to install it under raspbian/debian/ubuntu: apt-get install xutils-dev您可以查看makedepend ,将其安装在 raspbian/debian/ubuntu 下: apt-get install xutils-dev


Note the link is done each time you do make :请注意,每次您执行时都会完成链接make

pi@raspberrypi:/tmp/p $ find .
.
./bin
./bin/main
./src
./src/main.hpp
./src/main.cpp
./Makefile
./obj
./obj/main.o
pi@raspberrypi:/tmp/p $ make
./obj/main.o
g++ -std=c++14 -Wall obj/main.o -o ./bin/main
pi@raspberrypi:/tmp/p $ make
./obj/main.o
g++ -std=c++14 -Wall obj/main.o -o ./bin/main
pi@raspberrypi:/tmp/p $ 

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

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