我尝试根据教程构建以下 makefile。然后我(认为)我添加了必要的源以使其适合我的代码..但它找不到目标。我认为这可能与源的文件夹结构有关,但我无法弄清楚。
SVN_REV=(svnversion -cn | sed -e 's/.*://' -e 's/\([0-9]*\).*/\1/' | grep '[0-9]')
DEBUG_OPTS:=-g -DSVN_REV \
-DDEBUG_MODE \
-UTIME_MODE \
-UREADFROMFILE
CFLAGS:=$(DEBUG_OPTS) -c -Wall
LDFLAGS:=$(DEBUG_OPTS) -lpthread
EXECUTABLE:=fw_board
COMPILER:=arm-linux-gcc
(makefile 继续如下,从 TOPDIR 可以看到 src 文件位置)
SOURCES:=main_process/main.c \
ezxml/ezxml.c \
info_file_parser/info_file_parser.c \
info_file_parser/array_management.c \
info_file_parser/protocol_file_parser.c \
info_file_parser/sequence_file_parser.c \
info_file_parser/topology_file_parser.c \
seq_exec_module/seq_execution.c \
i2c_device_drivers/pca950x.c \
i2c_device_drivers/ltc2495.c \
i2c_device_drivers/ltc2609.c \
i2c_device_drivers/temperature_uc.c \
i2c_device_drivers/pcf8574.c \
i2c_device_drivers/stepper_motor_control.c \
i2c_device_drivers/test_functions.c \
i2c_device_drivers/stepper_pca.c \
i2c_device_drivers/stepper_atmega.c \
i2c_device_drivers/general_i2c.c \
i2c_device_drivers/zx_mbd_pwr.c \
i2c_device_drivers/virtual_switch_handler.c \
i2c_device_drivers/pressure_sensor_ASDXAV030PG7A5.c \
hashing/ConfigData.c \
hashing/Hash.c \
hashing/LinkedList.c \
init_file_parser/init_file_parser.c \
usb_communication/serial_comm.c \
protocol_parser/protocol_parser.c
BINDIR:=bin
OBJDIR:=.obj
OBJECTS:=$(addprefix $(OBJDIR)/,$(SOURCES:%.c=%.o))
.PHONY: all
all: $(SOURCES) $(EXECUTABLE)
@echo all_start
这是链接阶段
$(EXECUTABLE): $(OBJECTS) | $(BINDIR)
@echo link
$(COMPILER) $(LDFLAGS) $(OBJECTS) -o $(BINDIR)/$@
和编译阶段。但如您所见,无法找到 main.c
$(OBJECTS): $(OBJDIR)/%.o : %.c | $(OBJDIR)
@echo compile
$(COMPILER) $(CFLAGS) $< -o $@
$(OBJDIR):
mkdir $(OBJDIR)
$(BINDIR):
mkdir $(BINDIR)
.PHONY: clean
clean:
@echo removing_files
rm -rf $(OBJECTS) $(BINDIR)/$(EXECUTABLE)
运行 make 返回以下内容:
make: *** No rule to make target `main_process/main.c', needed by `all'. Stop.
最佳答案
不要将源列为所有源的依赖项。
全部:取决于$(EXECUTABLE)。顺便说一句,echo 只会在 $(EXECUTABLE) 构建之后才会获得输出(如果有的话)。
$(EXECUTABLE):取决于$(OBJECTS)
我不太确定 $(OBJECTS) 规则上的语法在做什么,但不需要在 $(OBJECTS) 上有明确的规则。只需让 $(OBJECTS) 列出所有目标文件,然后制定从 C 文件生成它们的规则就足够了。
根据所有这些规则,“all:”无论如何都隐式依赖于来源。另外,“all:”是一个“虚假”规则,因此每次都会重建所有依赖项。
路径 main_process/main.c 需要相对于包含 makefile 的目录存在。
解决所有这些问题,你就可以开始了。
关于c - make 找不到 make 目标的规则(手动生成 makefile 的新手),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7515695/