3

我有一块带有 Cortex M4F 的 Stellaris Launchpad 板。我想通过我自己的一些 C++ 代码在板上使用 FreeRTOS。然而,FreeRTOS 是用 C 语言编写的,因此不能用 G++ 编译;我试过了。

我在某处读到我应该用 编译 FreeRTOS gcc,然后用 编译我的应用程序代码g++,然后使用链接器将它们拉到一起。这是有道理的,但我真的很想知道如何将这种行为放在 Makefile 中。我发现这个 Makefile让我更加困惑。

目前,我只是使用随 Stellaris 演示中的 FreeRTOS 示例一起分发的 Makefile,但这只是为 C 代码设置的。这个问题问的问题类似于我所追求的,但我无法理解如何使用 GCC 编译 FreeRTOS 以及使用 G++ 编译我的其余代码。

FreeRTOS 显然将其代码包装在extern "C" { }指令中,但在编译 FreeRTOS 代码库时尝试使用 G++ 仍然失败。

我不喜欢在 SO 上发布大量代码,但我认为信息越多越好。Stellaris 演示有一个通用makedefs文件:

# Get the operating system name.  If this is Cygwin, the .d files will be
# munged to convert c: into /cygdrive/c so that "make" will be happy with the
# auto-generated dependencies.
os:=${shell uname -s}

# The compiler to be used.
ifndef COMPILER
COMPILER=gcc
endif

# Definitions for using GCC.
ifeq (${COMPILER}, g++)

# Get the prefix for the tools to use.  Use arm-stellaris-eabi if it exists,
# otherwise fall back to arm-none-eabi.
PREFIX=${shell type arm-stellaris-eabi-gcc > /dev/null 2>&1 && \
         echo arm-stellaris-eabi || echo arm-none-eabi}

# The command for calling the compiler.
CC=${PREFIX}-${COMPILER}

# The location of the C compiler
# ARMGCC_ROOT is used by some makefiles that need to know where the compiler
# is installed.  It is not used further for normal stellarisware apps
ARMGCC_ROOT:=${shell dirname '${shell sh -c "which ${CC}"}'}/..

# Determine the compiler CPU/FPU options based on the processor variant.
ifndef VARIANT
CPU=-mcpu=cortex-m3
FPU=
else
ifeq (${VARIANT}, cm3)
CPU=-mcpu=cortex-m3
FPU=
else
ifeq (${VARIANT}, cm4f)
CPU=-mcpu=cortex-m4
FPU=-mfpu=fpv4-sp-d16 -mfloat-abi=softfp
else
$(error Unknown processor variant ${VARIANT}!)
endif
endif
endif

# The flags passed to the assembler.
AFLAGS=-mthumb \
       ${CPU}  \
       ${FPU}  \
       -MD

# The flags passed to the compiler.
CFLAGS=-mthumb             \
       ${CPU}              \
       ${FPU}              \
       -Os                 \
       -ffunction-sections \
       -fdata-sections     \
       -MD                 \
       -Wall               \
       -pedantic           \
       -DPART_${PART}      \
       -c

# The command for calling the library archiver.
AR=${PREFIX}-ar

# The command for calling the linker.
LD=${PREFIX}-ld

# The flags passed to the linker.
LDFLAGS=--gc-sections

# Get the location of libgcc.a from the GCC front-end.
LIBGCC=${shell ${CC} ${CFLAGS} -print-libgcc-file-name}

# Get the location of libc.a from the GCC front-end.
LIBC=${shell ${CC} ${CFLAGS} -print-file-name=libc.a}

# Get the location of libm.a from the GCC front-end.
LIBM=${shell ${CC} ${CFLAGS} -print-file-name=libm.a}

# The command for extracting images from the linked executables.
OBJCOPY=${PREFIX}-objcopy

# Tell the compiler to include debugging information if the DEBUG environment
# variable is set.
ifdef DEBUG
CFLAGS+=-g -D DEBUG
endif

# Add the tool specific CFLAGS.
CFLAGS+=${CFLAGSgcc}

# Add the include file paths to AFLAGS and CFLAGS.
AFLAGS+=${patsubst %,-I%,${subst :, ,${IPATH}}}
CFLAGS+=${patsubst %,-I%,${subst :, ,${IPATH}}}

# The rule for building the object file from each C source file.
${COMPILER}${SUFFIX}/%.o: %.c
    @if [ 'x${VERBOSE}' = x ];                            \
     then                                                 \
         echo "  CC    ${<}";                             \
     else                                                 \
         echo ${CC} ${CFLAGS} -D${COMPILER} -o ${@} ${<}; \
     fi
    @${CC} ${CFLAGS} -D${COMPILER} -o ${@} ${<}
ifneq ($(findstring CYGWIN, ${os}), )
    @sed -i -r 's/ ([A-Za-z]):/ \/cygdrive\/\1/g' ${@:.o=.d}
endif

# The rule for building the object file from each assembly source file.
${COMPILER}${SUFFIX}/%.o: %.S
    @if [ 'x${VERBOSE}' = x ];                               \
     then                                                    \
         echo "  AS    ${<}";                                \
     else                                                    \
         echo ${CC} ${AFLAGS} -D${COMPILER} -o ${@} -c ${<}; \
     fi
    @${CC} ${AFLAGS} -D${COMPILER} -o ${@} -c ${<}
ifneq ($(findstring CYGWIN, ${os}), )
    @sed -i -r 's/ ([A-Za-z]):/ \/cygdrive\/\1/g' ${@:.o=.d}
endif

# The rule for creating an object library.
${COMPILER}${SUFFIX}/%.a:
    @if [ 'x${VERBOSE}' = x ];     \
     then                          \
         echo "  AR    ${@}";      \
     else                          \
         echo ${AR} -cr ${@} ${^}; \
     fi
    @${AR} -cr ${@} ${^}

# The rule for linking the application.
${COMPILER}${SUFFIX}/%.axf:
    @if [ 'x${SCATTERgcc_${notdir ${@:.axf=}}}' = x ];                    \
     then                                                                 \
         ldname="${ROOT}/${COMPILER}/standalone.ld";                      \
     else                                                                 \
         ldname="${SCATTERgcc_${notdir ${@:.axf=}}}";                     \
     fi;                                                                  \
     if [ 'x${VERBOSE}' = x ];                                            \
     then                                                                 \
         echo "  LD    ${@} ${LNK_SCP}";                                  \
     else                                                                 \
         echo ${LD} -T $${ldname}                                         \
              --entry ${ENTRY_${notdir ${@:.axf=}}}                       \
              ${LDFLAGSgcc_${notdir ${@:.axf=}}}                          \
              ${LDFLAGS} -o ${@} $(filter %.o %.a, ${^})                  \
              '${LIBM}' '${LIBC}' '${LIBGCC}';                            \
     fi;                                                                  \
    ${LD} -T $${ldname}                                                   \
          --entry ${ENTRY_${notdir ${@:.axf=}}}                           \
          ${LDFLAGSgcc_${notdir ${@:.axf=}}}                              \
          ${LDFLAGS} -o ${@} $(filter %.o %.a, ${^})                      \
          '${LIBM}' '${LIBC}' '${LIBGCC}'
    @${OBJCOPY} -O binary ${@} ${@:.axf=.bin}
endif

上面的文件是由它Makefile自己包含的(如下):

# Defines the part type that this project uses.
PART=LM4F120H5QR

# Set the processor variant.
VARIANT=cm4f

# The base directory for StellarisWare.
ROOT=../lib

COMPILER=gcc

# Include the common make definitions.
include ${ROOT}/makedefs

# Where to find source files that do not live in this directory.
VPATH=${ROOT}/FreeRTOS/Source/portable/GCC/ARM_CM4F
VPATH+=${ROOT}/FreeRTOS/Source/portable/MemMang/
VPATH+=${ROOT}/FreeRTOS/Source
VPATH+=${ROOT}/drivers
VPATH+=${ROOT}/utils

# Where to find header files that do not live in the source directory.
IPATH=.
IPATH+=..
IPATH+=${ROOT}
IPATH+=${ROOT}/FreeRTOS/Source/portable/GCC/ARM_CM4F
IPATH+=${ROOT}/FreeRTOS
IPATH+=${ROOT}/FreeRTOS/Source/include
IPATH+=${ROOT}

# The default rule, which causes the FreeRTOS example to be built.
all: ${COMPILER}
all: ${COMPILER}/freertos_demo.axf

# The rule to clean out all the build products.
clean:
    @rm -rf ${COMPILER} ${wildcard *~}

# The rule to create the target directory.
${COMPILER}:
    @mkdir -p ${COMPILER}

# Rules for building the FreeRTOS example.
${COMPILER}/freertos_demo.axf: ${COMPILER}/buttons.o
${COMPILER}/freertos_demo.axf: ${COMPILER}/freertos_demo.o
${COMPILER}/freertos_demo.axf: ${COMPILER}/heap_2.o
${COMPILER}/freertos_demo.axf: ${COMPILER}/led_task.o
${COMPILER}/freertos_demo.axf: ${COMPILER}/list.o
${COMPILER}/freertos_demo.axf: ${COMPILER}/port.o
${COMPILER}/freertos_demo.axf: ${COMPILER}/queue.o
${COMPILER}/freertos_demo.axf: ${COMPILER}/rgb.o
${COMPILER}/freertos_demo.axf: ${COMPILER}/startup_${COMPILER}.o
${COMPILER}/freertos_demo.axf: ${COMPILER}/switch_task.o
${COMPILER}/freertos_demo.axf: ${COMPILER}/tasks.o
${COMPILER}/freertos_demo.axf: ${COMPILER}/uartstdio.o
${COMPILER}/freertos_demo.axf: ${COMPILER}/ustdlib.o
${COMPILER}/freertos_demo.axf: ${ROOT}/driverlib/${COMPILER}-cm4f/libdriver-cm4f.a
${COMPILER}/freertos_demo.axf: freertos_demo.ld
SCATTERgcc_freertos_demo=freertos_demo.ld
ENTRY_freertos_demo=ResetISR
CFLAGSgcc=-DTARGET_IS_BLIZZARD_RA1

# Include the automatically generated dependency files.
ifneq (${MAKECMDGOALS},clean)
-include ${wildcard ${COMPILER}/*.d} __dummy__
endif

我的一些目录结构如下:

Projects/
    lib/
        FreeRTOS/
        driverlib/
        drivers/
        inc/
        utils/
        makedefs
    TestProject/
        loads.cpp
        of.cpp
        files.h
        here.h
        Makefile

如果我正确地说 FreeRTOS 必须使用 GCC 编译,然后与我的 C++ 项目代码链接,我该如何修改我当前的 Makefile 来做到这一点?我想要一个合理通用的解决方案,以便我可以在其他项目中重新使用它,但我对 C/C++ 构建过程或 Makefile 了解不多,因此我提出了这个(冗长的)问题。我不想/意思听起来像是在问“为我做这个”的问题,但尽管在 Google 和 SO 上搜索,我真的无法弄清楚如何做到这一点。

4

1 回答 1

0

我自己没有将 FreeRTOS 与 C++ 一起使用,但它已在FreeRTOS 论坛上讨论过很多次,并且在 FreeRTOS Interactive 站点中有一些 C++ 框架。

于 2012-11-19T09:58:20.863 回答