我有一个小项目,文件放在src文件夹中:functionAdd.cpp; functionSubtract.cpp; main.cpp和我的Makefile。在包含文件夹中,我得到了functionAdd.hpp和functionSubtract.hpp。首先,我有一个错误,例如,找不到这两个.hpp文件。引用此answer之后,我创建了新的Makefile。所以在这里我做了什么:
CC = g++
HEADER1 = home/administrator/Desktop/makefile-test/include/functionAdd.hpp
HEADER2 = home/administrator/Desktop/makefile-test/include/functionSubtract.hpp
CFLAGS = -c -Wall -Iinclude
all:calculation
calculation: main.o functionSubtract.o functionAdd.o
$(CC) main.o functionSubtract.o functionAdd.o -o calculation
main.o: main.cpp $(HEADER) $(HEADER2)
$(CC) $(CFLAGS) main.cpp
functionSubtract.o: functionSubtract.cpp $(HEADER2)
$(CC) $(CFLAGS) functionSubtract.cpp
functionAdd.o: functionAdd.cpp $(HEADER1)
$(CC) $(CFLAGS) functionAdd.cpp
clean:
rm -rf *o calculation
现在,错误消息是: make:***没有规则可以使目标目录home / administrator / Desktop / makefile-test / include / functionSubtract.hpp成为main.o所需要的。停止。
答案 0 :(得分:0)
假设源文件,头文件和Makefile的组织方式如下:
src/Makefile
src/*.cpp
include/*.hpp
然后您可以使用:
CC = g++
HEADER1 = ../include/functionAdd.hpp
HEADER2 = ../include/functionSubtract.hpp
CFLAGS = -c -Wall -I../include
.PHONY: all clean
all: calculation
calculation: main.o functionSubtract.o functionAdd.o
$(CC) $^ -o $@
%.o: %.cpp
$(CC) $(CFLAGS) $< -o $@
main.o: $(HEADER) $(HEADER2)
functionSubtract.o: $(HEADER2)
functionAdd.o: $(HEADER1)
clean:
rm -rf *o calculation
更加紧凑和易于维护。