I'm trying to run this makefile, and run into an issue. 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? 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. Here's my 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.
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:
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.