gpt4 book ai didi

带 print 的 CMake 构建后输出命令

转载 作者:行者123 更新时间:2023-12-02 22:28:08 25 4
gpt4 key购买 nike

我正在使用 CLion 1.2 为 STM32 目标构建嵌入式 C 项目。使用 GNU ARM 工具的交叉编译效果很好,但是,我想在构建可执行文件后运行 arm-none-eabi-size ,并将该命令的输出打印到构建中输出窗口。

我已经彻底了解了 add_custom_command 宏,但是,我并不打算生成输出文件,而这正是该宏的作用。

下面是我在项目中使用的CMakeLists.txt:

cmake_minimum_required(VERSION 3.3)
include(CMakeForceCompiler)
project(rtos_clion)
# -------------------------------------------------------------------

set(CMAKE_SYSTEM_PROCESSOR cortex-m0)
SET(CMAKE_SYSTEM_NAME Generic)

# Target Environment
SET(CMAKE_FIND_ROOT_PATH "C:/Program Files (x86)/GNU Tools ARM Embedded/4.9 2015q2")

# Cross compiler
CMAKE_FORCE_C_COMPILER(arm-none-eabi-gcc GNU)
CMAKE_FORCE_CXX_COMPILER(arm-none-eabi-g++ GNU)

# Executable type
set(CMAKE_EXECUTABLE_SUFFIX ".elf")

# Compiler flags
set(ARM_FLAGS "-mcpu=cortex-m0 -mthumb -Os")

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${ARM_FLAGS} -std=gnu++11 -fabi-version=0 -fno-exceptions -fno-rtti -fno-use-cxa-atexit -fno-threadsafe-statics -ffunction-sections -g")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu11 ${ARM_FLAGS} -ffunction-sections -fmessage-length=0 -fsigned-char -fdata-sections -ffreestanding -fno-move-loop-invariants -Wall -Wextra -g")
set(CMAKE_EXE_LINKER_FLAGS "${ARM_FLAGS} -g -Wl,--library-path=\"${PROJECT_SOURCE_DIR}\" -T mem.ld -T libs.ld -T sections.ld -Xlinker --gc-sections -Wl,-Map=${PROJECT_NAME}.map --specs=nano.specs")

set(CMAKE_C_LINK_EXECUTABLE "<CMAKE_C_COMPILER> <LINK_FLAGS> -o <TARGET> <OBJECTS>")
set(CMAKE_CXX_LINK_EXECUTABLE "<CMAKE_CXX_COMPILER> <LINK_FLAGS> -o <TARGET> <OBJECTS>")
# -------------------------------------------------------------------

# Global definitions
add_definitions(-DDEBUG -DUSE_FULL_ASSERT -DSTM32F051x8 -DHSE_VALUE=8000000)

include_directories(
# My include directories
)

set(SOURCE_FILES
# My source files
)

add_executable(${PROJECT_NAME} ${SOURCE_FILES})

# search for programs in the build host directories
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

这是在控制台中执行时arm-none-eabi-size打印的内容:

 $arm-none-eabi-size --format=berkeley "rtos_clion.elf"

text data bss dec hex filename
32840 308 7796 40944 9ff0 rtos_clion.elf

我如何运行它并在构建窗口中显示输出?

谢谢!

最佳答案

只需使用add_custom_commandTARGET流:

add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND arm-none-eabi-size --format=berkeley "rtos_clion.elf")

该命令将在(重新)构建可执行文件后执行。

关于带 print 的 CMake 构建后输出命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33825571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com