简体   繁体   English

如何避免必须手动从我的Makefile中的不同路径列出每个C文件?

[英]How can I avoid having to manually list every C-File from different paths in my makefile?

Currently I am having to list every single C-File in my makefile, which does not seem efficient. 当前,我必须列出我的makefile中的每个C文件,这似乎效率不高。 The C-Files exist in multiple folders and subfolders. C文件存在于多个文件夹和子文件夹中。

For example: 例如:

C_SOURCES =  \
Core/main.c \
Core/adc.c \
Core/buttons.c \
Core/config.c \
Core/delay.c \
Core/dsp/filter.c \
Core/ui/ui_definition.c \
... and so on
Core/system_stm32f4xx.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c \

I also wonder how to make it work so that the Linker is taking the correct object-files. 我也想知道如何使它起作用,以便链接器可以获取正确的目标文件。 At the moment all object-files are getting saved into the 'build'-directory, as shown below: 目前,所有目标文件都保存到“ build”目录中,如下所示:

all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin

# list of objects
OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o)))
vpath %.c $(sort $(dir $(C_SOURCES)))

# list of ASM program objects
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.s=.o)))
vpath %.s $(sort $(dir $(ASM_SOURCES)))

$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR) 
    $(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@

$(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR)
    $(AS) -c $(CFLAGS) $< -o $@

$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile
    $(CC) $(OBJECTS) $(LDFLAGS) -o $@
    $(SZ) $@

$(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
    $(HEX) $< $@

$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
    $(BIN) $< $@

I am also confused about how to change the .o-rule in the Makefile, in the case that the C-Files from the subfolders are also saved in the subfolders in the build directory. 我也对如何更改Makefile中的.o-rule感到困惑,如果子文件夹中的C文件也保存在build目录的子文件夹中。 (If the 'Core/dsp/filter.c' gets saved under 'build/dsp/filter.o' or 'build/filter.o', how do I need to change the rule accordingly?) (如果“ Core / dsp / filter.c”保存在“ build / dsp / filter.o”或“ build / filter.o”下,那么我该如何相应地更改规则?)

You've asked a lot of different questions here, without a lot of specificity, so I'll just answer your first, clear question: 您在这里问了很多不同的问题,没有太多的具体性,所以我将回答您的第一个明确的问题:

If you want make to find all the source files on its own rather than you listing them you can use the shell function to run find : 如果要使自己查找所有源文件而不是列出它们,可以使用shell函数运行find

C_SOURCES := $(shell find Core Drivers -name \*.c -print)

You need to use := here to ensure that the find command is not re-run every time the $(C_SOURCES) variable reference is expanded, but is instead only run one time. 您需要在此处使用:= ,以确保每次扩展$(C_SOURCES)变量引用时都不会重新运行find命令,而是仅运行一次。

This works for me, regarding the including all .c files from sub-folders and linking object files issues. 对于我来说,这对我来说是有效的,涉及包括子文件夹中的所有.c文件以及链接目标文件的问题。

#include .c files
SOURCES = $(shell echo ./**/*.c)

#obj file
OBJ = $(SOURCES:.c=.o)

Though I'm not sure if ./**/*.c works on Windows.. 虽然我不确定./**/*.c是否可以在Windows上运行。

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

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