更高级的makefile

时间:2015-11-19 10:16:28

标签: c makefile

我正在创建(至少我正在尝试)为软件项目创建一个makefile。

因为在我遇到问题之前我从未使用过make。

该软件项目包含许多包含源文件和头文件的子文件夹和子文件夹。

应该递归收集这些文件,然后使用横臂工具链进行链接。

当前的makefile如下所示:

    RM := rm -rf

# Every subdirectory with source files must be described here

DIR := \
    D:/foxmake/

SUBDIRS := \
    src/test                                     \
    src/test/usb_cdc_lolevel                     \
    src/os                                       \
    src/os/FreeRTOS/Source                       \
    src/os/FreeRTOS/Source/portable/MemMang      \
    src/os/FreeRTOS/Source/portable/GCC/ARM_CM4F \
    src/os/FreeRTOS/Source/CMSIS_RTOS            \
    src/module/uart                              \
    src/module/spi                               \
    src/module/rcc                               \
    src/module/ltc                               \
    src/module/irq                               \
    src/module/io                                \
    src/module/eeprom                            \
    src/module/dma                               \
    src/module/cpu                               \
    src/module/contactor                         \
    src/module/config                            \
    src/module/com                               \
    src/module/can                               \
    src/module/adc                               \
    src/hal/STM32F4xx_HAL_Driver/Src             \
    src/general                                  \
    src/general/config                           \
    src/engine/task                              \
    src/engine/sysctrl                           \
    src/engine/sof                               \
    src/engine/soc                               \
    src/engine/isoguard                          \
    src/engine/diag                              \
    src/engine/database                          \
    src/engine/config                            \
    src/application/task                         \
    src/application/config                       \

CFLAGS := \
    -mcpu=cortex-m4           \
    -mthumb -mlittle-endian   \
    -mfloat-abi=softfp        \
    -mfpu=fpv4-sp-d16         \
    -O0                       \
    -fmessage-length=0        \
    -fsigned-char             \
    -ffunction-sections       \
    -fdata-sections           \
    -ffreestanding            \
    -fno-move-loop-invariants \
    -Wall                     \
    -g3                       \
    -std=gnu11                \
    -DSTM32F429xx             \
    -DDEBUG                   \
    -DUSE_FULL_ASSERT         \
    -DTRACE                   \
    -DOS_USE_TRACE_ITM        \
    -DUSE_HAL_DRIVER          \
    -DHSE_VALUE=8000000

INCDIRS := \
    -I"D:/foxmake/src/application"                              \
    -I"D:/foxmake/src/application/config"                       \
    -I"D:/foxmake/src/application/task"                         \
    -I"D:/foxmake/src/module/adc"                               \
    -I"D:/foxmake/src/module/can"                               \
    -I"D:/foxmake/src/module/com"                               \
    -I"D:/foxmake/src/module/config"                            \
    -I"D:/foxmake/src/module/contactor"                         \
    -I"D:/foxmake/src/module/cpuload"                           \
    -I"D:/foxmake/src/module/io"                                \
    -I"D:/foxmake/src/module/ltc"                               \
    -I"D:/foxmake/src/module/spi"                               \
    -I"D:/foxmake/src/module/uart"                              \
    -I"D:/foxmake/src/engine/config"                            \
    -I"D:/foxmake/src/engine/database"                          \
    -I"D:/foxmake/src/engine/diag"                              \
    -I"D:/foxmake/src/engine/isoguard"                          \
    -I"D:/foxmake/src/engine/soc"                               \
    -I"D:/foxmake/src/engine/sof"                               \
    -I"D:/foxmake/src/engine/sysctrl"                           \
    -I"D:/foxmake/src/engine/task"                              \
    -I"D:/foxmake/src/general"                                  \
    -I"D:/foxmake/src/general/config"                           \
    -I"D:/foxmake/src/general/includes"                         \
    -I"D:/foxmake/src/hal/CMSIS/Device/ST/STM32F4xx/Include"    \
    -I"D:/foxmake/src/hal/CMSIS/Include"                        \
    -I"D:/foxmake/src/hal/STM32F4xx_HAL_Driver/Inc"             \
    -I"D:/foxmake/src/os"                                       \
    -I"D:/foxmake/src/os/FreeRTOS"                              \
    -I"D:/foxmake/src/os/FreeRTOS/Source"                       \
    -I"D:/foxmake/src/os/FreeRTOS/Source/include"               \
    -I"D:/foxmake/src/os/FreeRTOS/Source/CMSIS_RTOS"            \
    -I"D:/foxmake/src/os/FreeRTOS/Source/portable/GCC/ARM_CM4F" \
    -I"D:/foxmake/src/test"                                     \
    -I"D:/foxmake/src/module/cpu"                               \
    -I"D:/foxmake/src/module/dma"                               \
    -I"D:/foxmake/src/module/irq"                               \
    -I"D:/foxmake/src/module/rcc"                               \
    -I"D:/foxmake/src/test/usb_cdc_lolevel"

S_UPPER_SRCS := \
    $(DIR)src/general/config/startup_stm32f429xx.S

S_UPPER_DEPS := \
    ./src/general/config/startup_stm32f429xx.d

rwildcard = $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2) $(filter $(subst *,%,$2),$d))

# All of the sources participating in the build are defined here
C_SRCS := \
    $(call rwildcard, , *.c)
#$(info C_SRCS='$(C_SRCS)')
OBJDIR := objdir

OBJS := \
    $(patsubst %.c,%.o,$(C_SRCS))

#$(error OBJS='$(OBJS)')
# comment the above line once OBJS is ok.

C_DEPS := \
    $(patsubst %.c,%.d,$(C_SRCS))
#$(info C_DEPS='$(C_DEPS)')

%.o: %.c ,$(C_SRCS)
    echo 'Building file: $<'
    echo 'Invoking: Cross ARM C Compiler'
    arm-none-eabi-gcc $(CFLAGS) $(INCDIRS) -MMD -MP -MF"$(@:%.o=%.d)" -MT"$@" -c -o "$@" "$<"
    echo 'Finished building: $<'
    echo ' '

ifneq ($(MAKECMDGOALS),clean)
ifneq ($(strip $(CC_DEPS)),)
-include $(CC_DEPS)
endif
ifneq ($(strip $(C++_DEPS)),)
-include $(C++_DEPS)
endif
ifneq ($(strip $(C_UPPER_DEPS)),)
-include $(C_UPPER_DEPS)
endif
ifneq ($(strip $(CXX_DEPS)),)
-include $(CXX_DEPS)
endif
ifneq ($(strip $(ASM_DEPS)),)
-include $(ASM_DEPS)
endif
ifneq ($(strip $(S_UPPER_DEPS)),)
-include $(S_UPPER_DEPS)
endif
ifneq ($(strip $(C_DEPS)),)
-include $(C_DEPS)
endif
ifneq ($(strip $(CPP_DEPS)),)
-include $(CPP_DEPS)
endif
endif

# Add inputs and outputs from these tool invocations to the build variables
SECONDARY_FLASH := \
    foxbms.hex \

SECONDARY_LIST := \
    foxbms.lst \

SECONDARY_SIZE := \
    foxbms.siz \

# All Target
all: foxbms.elf secondary-outputs

# Tool invocations
foxbms.elf: $(OBJS) $(USER_OBJS)
    @echo 'Building target: $@'
    @echo 'Invoking: Cross ARM C++ Linker'
    arm-none-eabi-g++ -mcpu=cortex-m4 -mthumb -mlittle-endian -mfloat-abi=softfp -mfpu=fpv4-sp-d16 -O0 -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -ffreestanding -fno-move-loop-invariants -Wall -g3 -T "D:/foxmake/src/STM32F429ZIT6_FLASH.ld" -Xlinker --gc-sections -Wl,-Map,"foxbms.map" --specs=nano.specs -o "foxbms.elf" $(OBJS) $(USER_OBJS) $(LIBS)
    @echo 'Finished building target: $@'
    @echo ' '
    $(MAKE) --no-print-directory post-build

foxbms.hex: foxbms.elf
    @echo 'Invoking: Cross ARM GNU Create Flash Image'
    arm-none-eabi-objcopy -O ihex "foxbms.elf" "foxbms.hex"
    @echo 'Finished building: $@'
    @echo ' '

foxbms.lst: foxbms.elf
    @echo 'Invoking: Cross ARM GNU Create Listing'
    arm-none-eabi-objdump --source --all-headers --demangle --line-numbers --wide "foxbms.elf" > "foxbms.lst"
    @echo 'Finished building: $@'
    @echo ' '

foxbms.siz: foxbms.elf
    @echo 'Invoking: Cross ARM GNU Print Size'
    arm-none-eabi-size --format=berkeley $(OBJS) "foxbms.elf"
    @echo 'Finished building: $@'
    @echo ' '

# Other Targets
clean:
    -$(RM) \
        $(CC_DEPS)         \
        $(C++_DEPS)        \
        $(OBJS)            \
        $(C_UPPER_DEPS)    \
        $(CXX_DEPS)        \
        $(SECONDARY_FLASH) \
        $(SECONDARY_LIST)  \
        $(SECONDARY_SIZE)  \
        $(ASM_DEPS)        \
        $(S_UPPER_DEPS)    \
        $(C_DEPS)          \
        $(CPP_DEPS)        \
        foxbms.elf
    -@echo ' '

post-build:
    -@echo 'Create binary'
    -arm-none-eabi-objcopy -O binary "foxbms.elf" "foxbms.bin"
    -@echo ' '

secondary-outputs: $(SECONDARY_FLASH) $(SECONDARY_LIST) $(SECONDARY_SIZE)

.PHONY: all clean dependents x
.SECONDARY: post-build

使用eclipse执行这个makefile会给我这个错误:

make all 
cc -mcpu=cortex-m4 -mthumb -mlittle-endian -mfloat-abi=softfp -mfpu=fpv4-sp-d16 -O0 -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -ffreestanding -fno-move-loop-invariants -Wall -g3 -std=gnu11 -DSTM32F429xx -DDEBUG -DUSE_FULL_ASSERT -DTRACE -DOS_USE_TRACE_ITM -DUSE_HAL_DRIVER -DHSE_VALUE=8000000   -c -o src/general/nvic.o src/general/nvic.c
<builtin>: recipe for target 'src/general/nvic.o' failed
process_begin: CreateProcess(NULL, cc -mcpu=cortex-m4 -mthumb -mlittle-endian -mfloat-abi=softfp -mfpu=fpv4-sp-d16 -O0 -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -ffreestanding -fno-move-loop-invariants -Wall -g3 -std=gnu11 -DSTM32F429xx -DDEBUG -DUSE_FULL_ASSERT -DTRACE -DOS_USE_TRACE_ITM -DUSE_HAL_DRIVER -DHSE_VALUE=8000000 -c -o src/general/nvic.o src/general/nvic.c, ...) failed.
make (e=2): System is unable to find the given file.

make: *** [src/general/nvic.o] Error 2

有人知道为什么我的makefile失败了吗? 规则“%。o:%。c,$(C_SRCS)”有什么问题?

0 个答案:

没有答案