简体   繁体   English

Makefile:“没有规则来制作目标......”有多个工作目录

[英]Makefile: “No rule to make target…” with multiple working directories

I'm trying to run this makefile, and run into an issue. 我正在尝试运行此makefile,并遇到问题。 Make tells me 告诉我

"No rule to make target 'UDP_Server.o', needed by 'SendRawData',

Since I've given it the working directories of the files however, shouldn't the rule for %.o file work just fine? 既然我已经给它了文件的工作目录,那么%.o文件的规则不应该正常工作吗? I'm launching make within the /thing/asset/src directory, and I don't care where it puts the o files or the program, as long as I can access them. 我在/ thing / asset / src目录中启动make,并且我不关心它放置o文件或程序的位置,只要我可以访问它们。 Here's my makefile: 这是我的makefile:

CC = g++

INC += -I/home/pi/thing/ 
INC += -I/home/pi/thing/Asset/src/
INC += -I/home/pi/thing/Server/src/
INC += -I/home/pi/thing/Shared/NetworkInterface/src/
INC += -I/home/pi/mercuryapi/c/src/

LIB = /home/pi/mercury/c/src/api/

CFLAGS  = -std=c++11 -Wno-write-strings
LDFLAGS = -L$(LIB) -static -l libmercuryapi 

SOURCES += UDP_Client.cpp
SOURCES += UDP_Server.cpp
SOURCES += rawData.cpp
SOURCES += packetMethods.cpp
SOURCES += parseData.cpp
SOURCES += SendRawData.cpp

OBJECTS =  $(SOURCES:.cpp=.o)

DEPS  = UDP_Client.h
DEPS += UDP_Server.h
DEPS += packet.h
DEPS += rawData.h
DEPS += packetMethods.h
DEPS += parseData.h
DEPS += tm_reader.h

default: SendRawData

%.o: %.cpp $(SOURCES) $(DEPS)
    $(CC) $(CFLAGS) $(INC) -c $< -o $@ 

SendRawData: $(OBJECTS)
    $(CC) $(CFLAGS) $(LDFLAGS) $(INC) $< -o SendRawData

client: cmain.cpp UDP_Client.cpp
    $(CC) $(CFLAGS) cmain.cpp UDP_Client.cpp -o client

.cpp.o:
    $(CC) $(CFLAGS) $(INC) -c $< -o $@

.PHONY: clean
clean:
    rm *.o

If it helps at all, here is the directory structure visualized: 如果它有帮助,这里是可视化的目录结构:

/home/pi/thing/
├── Asset
│   ├── README.txt
│   └── src
│       ├── makefile
│       ├── README.txt
│       ├── SendRawData.cpp
│       ├── UDP_Client.cpp
│       └── UDP_Client.h
├── Server
│   └── src
│       ├── README.txt
│       ├── server
│       ├── UDP_Server.cpp
│       ├── UDP_Server.h
│       └── UDP_Server.o
└── Shared
    ├── NetworkInterface
    │   ├── README.txt
    │   └── src
    │       ├── header.h
    │       ├── packet.h
    │       ├── packetMethods.cpp
    │       ├── packetMethods.h
    │       ├── parseData.cpp
    │       ├── parseData.h
    │       ├── rawData.cpp
    │       ├── rawData.h
    │       └── testing.cpp
    └── README.txt

Make cares where you put the files. 小心放置文件的位置。 You need to tell make where the source files are located. 您需要告诉make源文件的位置。

The ugly, tedious, but explanatory solution is to add the path when specifying the dependency. 丑陋,乏味但解释性的解决方案是在指定依赖项时添加路径。 Instead of 代替

SOURCES += UDP_Client.cpp

write

SOURCES += ../../Server/src/UDP_Client.cpp

The beautiful and simple solution is to use makes VPATH variable: 美丽而简单的解决方案是使用make VPATH变量:

https://www.gnu.org/software/make/manual/html_node/General-Search.html

For example 例如

VPATH = "../../Server/src/UDP_Client.cpp:<list of : separated paths>"

This solution might only work if your using gnu make. 此解决方案可能仅在您使用gnu make时才有效。

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

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