2

我最近将我的 stm32 项目切换到 CMake 以独立于 IDE。根存储库(应用程序)包含多个子模块(HAL、FreeRTOS 等),其 CMakeLists.txt 明确包含每个使用的文件:

set(EXECUTABLE ${PROJECT_NAME}.elf)

add_executable(${EXECUTABLE}
    
    # Own sources
    src/main.c
    src/SEGGER_SYSVIEW_Config_FreeRTOS.c
    src/startup_stm32h723zgtx.s
    src/stm32h7xx_hal_timebase_tim.c
    src/system_stm32h7xx.c

    # Base CMSIS and HAL library
    lib-hal/stm32h7xx/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_tim.c
    lib-hal/stm32h7xx/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_tim_ex.c
    lib-hal/stm32h7xx/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_uart.c
    lib-hal/stm32h7xx/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_rcc.c
    lib-hal/stm32h7xx/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_rcc_ex.c
            
    #long list of HAL c files there...

    # FreeRTOS library
    lib-freertos/croutine.c
    lib-freertos/event_groups.c
    lib-freertos/list.c
    lib-freertos/queue.c
    lib-freertos/stream_buffer.c
    lib-freertos/tasks.c
    lib-freertos/timers.c
    lib-freertos/portable/GCC/ARM_CM7/r0p1/port.c
    lib-freertos/trace/Sample/FreeRTOSV10/SEGGER_SYSVIEW_FreeRTOS.c
    lib-freertos/trace/SEGGER/Syscalls/SEGGER_RTT_Syscalls_GCC.c
    lib-freertos/trace/SEGGER/SEGGER_RTT_ASM_ARMv7M.S
    lib-freertos/trace/SEGGER/SEGGER_RTT_printf.c
    lib-freertos/trace/SEGGER/SEGGER_RTT.c
    lib-freertos/trace/SEGGER/SEGGER_SYSVIEW.c
    )
    
target_include_directories(${EXECUTABLE}
    PRIVATE
    include
    src
    
    lib-hal/stm32h7xx/CMSIS/Include
    lib-hal/stm32h7xx/CMSIS/Device/ST/STM32H7xx/Include
    lib-hal/stm32h7xx/STM32H7xx_HAL_Driver/Inc
    
    lib-freertos/include
    lib-freertos/trace/Config
    lib-freertos/trace/SEGGER
    lib-freertos/trace/Sample/FreeRTOSV10/
    lib-freertos/portable/GCC/ARM_CM7/r0p1
    )

该解决方案有效,但我知道这不是一种可持续的方法。所以我尝试在 lib-hal 和 lib-freertos 子模块中创建库,指定它们的来源和包含

add_library(lib-hal-stm32h7xx)

target_include_directories(lib-hal-stm32h7xx
    PUBLIC
    CMSIS/Include
    CMSIS/Device/ST/STM32H7xx/Include
    STM32H7xx_HAL_Driver/Inc
    PRIVATE
    STM32H7xx_HAL_Driver/Src
)

target_sources(lib-hal-stm32h7xx
    PRIVATE
    STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_tim.c
    STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_tim_ex.c
    STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_uart.c
    STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_rcc.c
    STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_rcc_ex.c

    #long list of HAL c files there...
)

然后使用

add_subdirectory(lib-hal/stm32h7xx)
add_subdirectory(lib-freertos)

target_link_library(${EXECUTABLE} lib-freertos lib-hal-stm32h7xx)

将子模块“导入”到应用程序项目中。但是在构建可执行文件时,gcc 无法访问位于根目录include中的文件stm32h7xx_hal_conf.hFreeRTOSConfig.h。我不想将配置标头放入子模块中,因为它们用于具有不同配置的多个项目中。是否可以在将库添加到父项目后以某种方式扩展已指定的库目录搜索范围?

项目文件结构:

-src
-include (configuration for lib-hal and lib-freertos included there)
 -lib-hal
  -includes...
  -sources...
 -lib-freertos
  -includes...
  -sources...

提前感谢您的回复。

4

1 回答 1

0

正如 Tsyvarev 在评论中提到的,您可以在项目中修改目标的属性。为了保持干净,我通常为此创建一个函数并将其放在单独的文件中。

提示:您还可以将源文件添加到目标。对于 FreeRTOS,您可以添加特定于架构的文件,以防您的所有项目不在同一个 MCU 系列上运行。

function(configure_freertos target_name)
    target_sources(${target_name}
        PRIVATE
            lib-freertos/portable/GCC/ARM_CM7/r0p1/port.c
    )

    target_include_directories(${target_name}
        PUBLIC
            include
            lib-freertos/portable/GCC/ARM_CM7/r0p1
    )
endfunction()
于 2022-02-12T00:03:36.680 回答