将Tiva C系列的Makefile转换为CMakeLists.txt [英] Converting a Makefile to CMakeLists.txt for Tiva C Series

查看:148
本文介绍了将Tiva C系列的Makefile转换为CMakeLists.txt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用CMake构建过程在C ++中对Tiva C系列LaunchPad板进行编程.我下载了一个简单的示例来闪烁使用make构建的RGB LED,我希望能够使用cmake来启动更大的项目.

I'd like to program my Tiva C Series LaunchPad board in C++ using CMake build process. I downloaded a simple examples to blink the RGB LED I built using make and I'd like to be able to use cmake to start a bigger project.

以下是示例中提供的Makefile:

Here is the Makefile provided in the example :

# Tiva Makefile
# #####################################
#
# Part of the uCtools project
# uctools.github.com
#
#######################################
# user configuration:
#######################################
# TARGET: name of the output file
TARGET = firmware
# MCU: part number to build for
MCU = TM4C123GH6PM
# SOURCES: list of input source sources
SOURCES = main.c startup_gcc.c
# INCLUDES: list of includes, by default, use Includes directory
INCLUDES = -IInclude
# OUTDIR: directory to use for output
OUTDIR = build
# TIVAWARE_PATH: path to tivaware folder
TIVAWARE_PATH = ../tivaware

# LD_SCRIPT: linker script
LD_SCRIPT = $(MCU).ld

# define flags
CFLAGS = -g -mthumb -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=softfp
CFLAGS +=-Os -ffunction-sections -fdata-sections -MD -std=c99 -Wall
CFLAGS += -pedantic -DPART_$(MCU) -c -I$(TIVAWARE_PATH)
CFLAGS += -DTARGET_IS_BLIZZARD_RA1
LDFLAGS = -T $(LD_SCRIPT) --entry ResetISR --gc-sections

#######################################
# end of user configuration
#######################################
#
#######################################
# binaries
#######################################
CC = arm-none-eabi-gcc
LD = arm-none-eabi-ld
OBJCOPY = arm-none-eabi-objcopy
RM      = rm -f
MKDIR   = mkdir -p
#######################################

# list of object files, placed in the build directory regardless of source path
OBJECTS = $(addprefix $(OUTDIR)/,$(notdir $(SOURCES:.c=.o)))

# default: build bin
all: $(OUTDIR)/$(TARGET).bin

$(OUTDIR)/%.o: src/%.c | $(OUTDIR)
    $(CC) -o $@ $^ $(CFLAGS)

$(OUTDIR)/a.out: $(OBJECTS)
    $(LD) -o $@ $^ $(LDFLAGS)

$(OUTDIR)/$(TARGET).bin: $(OUTDIR)/a.out
    $(OBJCOPY) -O binary $< $@

# create the output directory
$(OUTDIR):
    $(MKDIR) $(OUTDIR)

clean:
    -$(RM) $(OUTDIR)/*

.PHONY: all clean

基于它的我的第一个CMakeLists.txt文件:

My first CMakeLists.txt file based on it :

project(firmware)
cmake_minimum_required(VERSION 2.8)

# this one is important
set(CMAKE_SYSTEM_NAME Generic)
#this one not so much
#set(CMAKE_SYSTEM_VERSION 1)

# specify the toolchain
set(TOOLCHAIN_PREFIX ${PROJECT_SOURCE_DIR}/../toolchain/bin/arm-none-eabi-)
set(CMAKE_C_COMPILER   ${TOOLCHAIN_PREFIX}gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}g++)
set(CMAKE_OBJCOPY ${TOOLCHAIN_PREFIX}objcopy)
set(CMAKE_AR ${TOOLCHAIN_PREFIX}ar)

# set compiler flags
set(MCU TM4C123GH6PM)
set(COMMON_FLAGS "-mthumb -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=softfp \
    -ffunction-sections -fdata-sections -pedantic \
    -MD -DPART_${MCU} -DTARGET_IS_BLIZZARD_RA1")

set(CMAKE_C_FLAGS_DEBUG "-g -Wall ${COMMON_FLAGS}")
set(CMAKE_CXX_FLAGS_DEBUG "-g -Wall -std=c++11 ${COMMON_FLAGS}")
set(CMAKE_C_FLAGS_RELEASE "-O2 -DNOTEST ${COMMON_FLAGS}")
set(CMAKE_CXX_FLAGS_RELEASE "-O2 -std=c++11 -DNOTEST ${COMMON_FLAGS}")

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

# add TivaWare header files to the project
include_directories(${PROJECT_SOURCE_DIR}/../tivaware)

# add source files to the project
aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})

# set linker flags
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS)
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS)
set_target_properties(${PROJECT_NAME}
    PROPERTIES
    LINK_FLAGS "-T ${MCU}.ld --entry ResetISR --gc-sections"
)

# define objcopy macro
macro(OBJCOPY_FILE EXE_NAME)
    set(FO ${CMAKE_CURRENT_BINARY_DIR}/${EXE_NAME}.bin)
    set(FI ${CMAKE_CURRENT_BINARY_DIR}/${EXE_NAME})
    message(STATUS ${FO})
    add_custom_command(
        OUTPUT ${FO}
        COMMAND ${CMAKE_OBJCOPY}
        ARGS -O binary -I elf32-little ${FI} ${FO}
        DEPENDS ${FI}
    )
    get_filename_component(TGT "${EXE_NAME}" NAME)
    add_custom_target("target-objcopy_${TGT}" ALL DEPENDS ${FO} VERBATIM)
    get_directory_property(extra_clean_files ADDITIONAL_MAKE_CLEAN_FILES)
    set_directory_properties(
        PROPERTIES
        ADDITIONAL_MAKE_CLEAN_FILES "${extra_clean_files};${FO}"
    )
    set_source_files_properties("${FO}" PROPERTIES GENERATED TRUE)
endmacro(OBJCOPY_FILE)

# set the objcopy for binary file
objcopy_file(${PROJECT_NAME})

它通过了CMake步骤,但是当我尝试使用make进行编译时,我得到了

It passes the CMake step but when I try to compile using make, I get

arm-none-eabi-g++: error: unrecognized command line option '--gc-sections'

我猜链接器标志应该改为与arm-none-eabi-ld一起使用.我该怎么办?

I guess the linkers flags should be used with arm-none-eabi-ld instead. How do I do this ?

我仍然不知道如何设置正确的链接器exe和标志,但是我发现CMake在firmware.dir/link.txt中生成了一个文件.它的内容是

I still have no idea how to set correct linker exe and flags but I found that CMake generates a file in firmware.dir/link.txt. Its content is

~/Documents/crh-2016/src/tiva/firmware/../toolchain/bin/arm-none-eabi-g++   -O2 -std=c++11 -fno-exceptions -DNOTEST -mthumb -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=softfp     -ffunction-sections -fdata-sections -pedantic     -MD -DPART_TM4C123GH6PM -DTARGET_IS_BLIZZARD_RA1    -T ~/Documents/crh-2016/src/tiva/firmware/TM4C123GH6PM.ld --entry ResetISR --gc-sections CMakeFiles/firmware.dir/main.cpp.o CMakeFiles/firmware.dir/startup_gcc.cpp.o  -o firmware  

我将其编辑为我要临时解决的问题

And I edited it to what I want to fix temporary this issue

~/Documents/crh-2016/src/tiva/firmware/../toolchain/bin/arm-none-eabi-ld -T ~/Documents/crh-2016/src/tiva/firmware/TM4C123GH6PM.ld --entry ResetISR --gc-sections CMakeFiles/firmware.dir/main.cpp.o CMakeFiles/firmware.dir/startup_gcc.cpp.o  -o firmware

但是LD似乎不喜欢G ++生成的.o文件,因为make表示

But it seems that LD doesn't like .o files generated by G++ because a make says

toolchain/bin/arm-none-eabi-ld: warning: cannot find entry symbol ResetISR; defaulting to 00000000

推荐答案

将我的评论变成答案

如Marc Glisse在 cflags'-Wl,-export-dynamic'与链接器标志' -export-dynamic'

As mentioned by in Marc Glisse in his comment you could pass linker flags in CMAKE_EXE_LINKER_FLAGS with -Wl,XXX, see e.g. cflags '-Wl,-export-dynamic' vs linker flags '-export-dynamic'

然后,您无需将链接器命令更改为ld.

Then you won't need to change the linker command to ld.

为了与makefile兼容,可以使用CMAKE_LINKERCMAKE_CXX_LINK_EXECUTABLE变量来更改链接器命令,例如对ld的调用.

For the comparibility with your makefile you can use the CMAKE_LINKER and CMAKE_CXX_LINK_EXECUTABLE variables to change the linker command like call to ld.

关于您的问题:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆