将标题包括在makefile中

Include headers in MakeFile

本文关键字:makefile 包括 标题      更新时间:2023-10-16

我试图将我的标题文件包括在makefile中,因此每次编辑没有相应的CPP文件的标头文件时,我都不必make clean && make

编辑

到目前为止,我有这个:

# Declaration of variables
CC = g++
CC_FLAGS = -Wall -g 
# File names
EXEC = Main
DEPS = MoveLogic.hpp
SOURCES = $(wildcard *.cpp) $(wildcard Pieces/*.cpp)
TARGET = $(SOURCES:.cpp=.o) 
TARGET_DEPS = $(DEPS:.hpp=.o) 
# Main target
$(EXEC): $(TARGET) $(TARGET_DEPS)
    $(CC) $(TARGET) $(TARGET_DEPS) -o $(EXEC)
# To obtain dependanies
%.o: %.hpp
    $(CC) $(DEPS) -o $@
# To obtain object files
%.o: %.cpp
    $(CC) -c $(CC_FLAGS) $< -o $@
# To remove generated files
clean:
    rm -f $(EXEC) $(TEST) $(TARGET)

但是我遇到了错误

 ignoring file MoveLogic.o, file was built for unsupported file format ( 0x43 0x50 0x43 0x48 0x01 0x0C 0x00 0x00 0x27 0x08 0x00 0x00 0x0B 0x02 0x68 0x42 ) which is not the architecture being linked (x86_64): MoveLogic.o

您不得将标题添加到SOURCE变量:所有不是.cpp文件的元素都直接插入将被清洁的TARGET

如果标题名称对应于您可以使用的源名称:

# To obtain object files
%.o: %.cpp %.h
    $(CC) -c $(CC_FLAGS) $< -o $@

但是,您会错过通用标头文件。最好的方法是按照Keithsmith的建议使用自动辩论。