ARM 上的 Cmake(arm-none-eabi)-配置

ARM 上的 Cmake(arm-none-eabi)-配置

我使用 cmake 为 arm cortex m3 (arm-none-eabi-) 编译我的项目。

我想添加以下选项:

1.)链接器应该生成二进制文件的.map文件。

2.) 除了我目前看到的 stderr /stdout 之外,make(Unix Makefiles)还应该将警告和错误存储到单独的文件中。

3.)配置“set(CMAKE_EXE_LINKER_FLAGS "-specs=nano.specs -Wl,--gc-sections -o myproject.elf" CACHE INTERNAL "")”没有生成我的 elf,所以我手动将输出复制到 output.elf。

有人能帮我吗?此外,如果你们经验丰富,能给我一些关于如何做得更好的提示就太好了。我目前在 windows10 (gnu make、cmake、git bash) 和 linux mint 上运行 cmake。

编译文件

cmake \
-G "Unix Makefiles" \
-D CMAKE_BUILD_TYPE=Debug \
..
make -j8

cmake_arm_none_eabi.cmake

set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)

set(CMAKE_ASM_COMPILER arm-none-eabi-gcc)
set(CMAKE_C_COMPILER arm-none-eabi-gcc)
set(CMAKE_CXX_COMPILER arm-none-eabi-g++)

set(CMAKE_EXE_LINKER_FLAGS "-specs=nano.specs -Wl,--gc-sections -o myproject.elf" CACHE INTERNAL "")

set(CMAKE_ASM_COMPILER_ID GNU)
set(CMAKE_C_COMPILER_ID GNU)
set(CMAKE_CXX_COMPILER_ID GNU)

set(CMAKE_ASM_COMPILER_FORCED TRUE)
set(CMAKE_C_COMPILER_FORCED TRUE)
set(CMAKE_CXX_COMPILER_FORCED TRUE)

CMakeLists.txt

include("cmake_arm_none_eabi.cmake")

cmake_minimum_required(VERSION 3.10.1)

project(myproject)

ENABLE_LANGUAGE(ASM)

SET(CMAKE_ASM_FLAGS "${CFLAGS} -x assembler-with-cpp")

set(LINKER_SCRIPT "mylinker.ld")

set(CPU_OPTIONS -mthumb -mcpu=cortex-m3)

SET(CMAKE_ASM_FLAGS_DEBUG "-g")
set(CMAKE_C_FLAGS_DEBUG "-Og -g -DDEBUG")
set(CMAKE_CXX_FLAGS_DEBUG "-Og -g")

add_compile_options(
    ${CPU_OPTIONS}
    $<$<COMPILE_LANGUAGE:C>:-std=gnu11> #c11>
    $<$<COMPILE_LANGUAGE:CXX>:-std=gnu++17> #c++17>
    #$<$<COMPILE_LANGUAGE:CXX>:-fms-extensions>
    $<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>
    $<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>
    $<$<COMPILE_LANGUAGE:CXX>:-fno-use-cxa-atexit>
    $<$<COMPILE_LANGUAGE:CXX>:-fno-threadsafe-statics>
    $<$<COMPILE_LANGUAGE:CXX>:-Wswitch-default>
    $<$<COMPILE_LANGUAGE:CXX>:-Wswitch-enum>
    $<$<COMPILE_LANGUAGE:CXX>:-Wmissing-include-dirs>
    -fstrict-volatile-bitfields
    -ffunction-sections
    -fdata-sections
    -fno-threadsafe-statics
    -fdce

    -Wfatal-errors
    -Wall
    -Wextra
    -Wcast-align
    -Wconversion
    -Wsign-conversion
    -Wold-style-cast
    -Wshadow
    -Wlogical-op
    -Wsuggest-override
    -Wsuggest-final-types
    -Wsuggest-final-methods
    -pedantic
)

include_directories(
    ${CMAKE_SOURCE_DIR}
)

link_directories(
    ${CMAKE_SOURCE_DIR}
)

add_executable(${PROJECT_NAME}
    mysource.c
    mysource2.cpp
    ...

target_link_libraries(${PROJECT_NAME}
    ${CPU_OPTIONS}
    -T${LINKER_SCRIPT}
    -nostartfiles
    m #libm
    gcc #libgcc
)

set_property(TARGET ${PROJECT_NAME} PROPERTY LINK_DEPENDS ${CMAKE_SOURCE_DIR}/${LINKER_SCRIPT})

答案1

为什么不直接添加带有-Wl,-Map=$(NAME).map参数的链接选项呢?只需检查一下\share\gcc-arm-none-eabi\samples\startupgcc 源文件夹的目录即可。

相关内容