From makefile to Cmake - stm32

徘徊边缘 提交于 2021-02-08 11:11:01

问题


I would like to test CLion for my stm32 project since it now support remote debugging! To do so I need to setup Cmake for my project and it is my issue.

I tried using this link which seems to be depreciated so I made some changes. It almost builds but there is a problem with the .elf .bin .hex.

CMakeLists.txt:

project(F466cmake)
cmake_minimum_required(VERSION 3.10)

add_definitions(-DSTM32F446xx)

file(GLOB_RECURSE USER_SOURCES "Src/*.c")
file(GLOB_RECURSE HAL_SOURCES "Drivers/STM32F4xx_HAL_Driver/Src/*.c")

add_library(CMSIS
        Drivers/CMSIS/Device/Src/system_stm32f4xx.c
        Drivers/CMSIS/Device/Src/startup_stm32f446xx.s)

include_directories(Inc)
include_directories(Drivers/STM32F4xx_HAL_Driver/Inc)
include_directories(Drivers/CMSIS/Inc)
include_directories(Drivers/CMSIS/Device/Inc)

set(CMAKE_C_COMPILER arm-none-eabi-gcc)
set(CMAKE_CXX_COMPILER arm-none-eabi-g++)
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)

set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/STM32F446RETx_FLASH.ld)
set(COMMON_FLAGS "-mcpu=cortex-m4 -mthumb -mthumb-interwork -mfloat-abi=hard -mfpu=fpv4-sp-d16 -ffunction-sections -fdata-sections -g -fno-common -fmessage-length=0")
set(CMAKE_CXX_FLAGS "${COMMON_FLAGS} -std=c++11")
set(CMAKE_C_FLAGS "${COMMON_FLAGS} -std=gnu99")
set(CMAKE_EXE_LINKER_FLAGS "-Wl,-gc-sections -T ${LINKER_SCRIPT}")

add_executable(${PROJECT_NAME}.elf ${USER_SOURCES} ${HAL_SOURCES} ${LINKER_SCRIPT})

target_link_libraries(${PROJECT_NAME}.elf CMSIS)

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-Map=${PROJECT_SOURCE_DIR}/build/${PROJECT_NAME}.map")
set(HEX_FILE ${PROJECT_SOURCE_DIR}/build/${PROJECT_NAME}.hex)
set(BIN_FILE ${PROJECT_SOURCE_DIR}/build/${PROJECT_NAME}.bin)

add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD
        COMMAND ${CMAKE_OBJCOPY} -Oihex $<TARGET_FILE:${PROJECT_NAME}.elf> ${HEX_FILE}
        COMMAND ${CMAKE_OBJCOPY} -Obinary $<TARGET_FILE:${PROJECT_NAME}.elf> ${BIN_FILE}
        COMMENT "Building ${HEX_FILE} \nBuilding ${BIN_FILE}")

terminal output:

[ 95%] Building C object CMakeFiles/F466cmake.elf.dir/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c.o
[100%] Linking C executable F466cmake.elf
/home/flo/bin/gcc-arm-none-eabi-7-2018-q2-update/bin/../lib/gcc/arm-none-eabi/7.3.1/../../../../arm-none-eabi/bin/ld: warning: cannot find entry symbol Reset_Handler; defaulting to 0000000008000000
Building /home/flo/STM32Cube/Testing/build/F466cmake.hex 
Building /home/flo/STM32Cube/Testing/build/F466cmake.bin
/usr/bin/objcopy: Unable to recognise the format of the input file `/home/flo/STM32Cube/Testing/build/F466cmake.elf'
CMakeFiles/F466cmake.elf.dir/build.make:563: recipe for target 'F466cmake.elf' failed
make[2]: *** [F466cmake.elf] Error 1
make[2]: *** Deleting file 'F466cmake.elf'
CMakeFiles/Makefile2:104: recipe for target 'CMakeFiles/F466cmake.elf.dir/all' failed
make[1]: *** [CMakeFiles/F466cmake.elf.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2


回答1:


Got it to work, thanks to Chris for the guidance. The issue was the ASM was not build and I had to link arm object copy.

CMakeLists.txt:

project(F466cmake C ASM)
cmake_minimum_required(VERSION 3.10)

add_definitions(-DSTM32F446xx)

file(GLOB_RECURSE USER_SOURCES "Src/*.c")
file(GLOB_RECURSE HAL_SOURCES "Drivers/STM32F4xx_HAL_Driver/Src/*.c")

add_library(CMSIS
        Drivers/CMSIS/Device/Src/system_stm32f4xx.c)
add_library(STARTUP
    Drivers/CMSIS/Device/Src/startup_stm32f446xx.s)

set_source_files_properties(Drivers/CMSIS/Device/Src/startup_stm32f446xx.s PROPERTIES COMPILE_FLAGS "-x assembler-with-cpp")
set_property(SOURCE Drivers/CMSIS/Device/Src/startup_stm32f446xx.s PROPERTY LANGUAGE C)

include_directories(Inc)
include_directories(Drivers/STM32F4xx_HAL_Driver/Inc)
include_directories(Drivers/CMSIS/Inc)
include_directories(Drivers/CMSIS/Device/Inc)

set(CMAKE_C_COMPILER arm-none-eabi-gcc)
set(CMAKE_CXX_COMPILER arm-none-eabi-g++)
set(OBJCOPY arm-none-eabi-objcopy)
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)

set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/STM32F446RETx_FLASH.ld)
set(COMMON_FLAGS "-mcpu=cortex-m4 -mthumb -mthumb-interwork -mfloat-abi=hard -mfpu=fpv4-sp-d16 -ffunction-sections -fdata-sections -g -fno-common -fmessage-length=0")
set(CMAKE_CXX_FLAGS "${COMMON_FLAGS} -std=c++11")
set(CMAKE_C_FLAGS "${COMMON_FLAGS} -std=gnu99")
set(CMAKE_EXE_LINKER_FLAGS "-Wl,-gc-sections -T ${LINKER_SCRIPT}")

add_executable(${PROJECT_NAME}.elf ${USER_SOURCES} ${HAL_SOURCES} ${LINKER_SCRIPT})

target_link_libraries(${PROJECT_NAME}.elf CMSIS STARTUP)

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-Map=${PROJECT_SOURCE_DIR}/build/${PROJECT_NAME}.map")
set(HEX_FILE ${PROJECT_SOURCE_DIR}/build/${PROJECT_NAME}.hex)
set(BIN_FILE ${PROJECT_SOURCE_DIR}/build/${PROJECT_NAME}.bin)

add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD
        COMMAND ${OBJCOPY} -Oihex $<TARGET_FILE:${PROJECT_NAME}.elf> ${HEX_FILE}
        COMMAND ${OBJCOPY} -Obinary $<TARGET_FILE:${PROJECT_NAME}.elf> ${BIN_FILE}
        COMMENT "Building ${HEX_FILE} \nBuilding ${BIN_FILE}")


来源:https://stackoverflow.com/questions/57348819/from-makefile-to-cmake-stm32

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!