使用GCC编译FreeRTOS,使用G++编译应用程序代码

3
我有一块带有Cortex M4F的Stellaris Launchpad开发板。我想在该板上使用FreeRTOS和一些自己的C++代码。但是,由于FreeRTOS是用C语言编写的,所以无法通过G++编译;我已经尝试过了。
我在某个地方看到,应该使用gcc编译FreeRTOS,然后使用g++编译我的应用程序代码,最后使用链接器将它们组合起来。这很有道理,但我真的很难在Makefile中实现这种行为。我找到了this 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知之甚少,因此我提出了这个(冗长的)问题。我不是想让你替我完成这个任务,但尽管在谷歌和Stack Overflow上搜索,我还是无法弄清楚怎样做。

在快速扫描makefile后,我建议将环境变量COMPILER设置为g ++,但是我并不非常擅长makefile... - PiotrNycz
@PiotrNycz 我忘了提到我已经尝试过了,但是由于FreeRTOS是用C语言编写的,所以无法编译。 - Bojangles
1个回答

0

我个人不使用C++的FreeRTOS,但是在FreeRTOS论坛上已经讨论了很多次,并且在FreeRTOS交互网站上有一些C++框架。


1
谢谢您的建议,但是经过快速搜寻,这些论坛似乎充满了无人回答的问题和旧帖子 :(。 - Bojangles
1
这个怎么样?链接 - peter_mcc

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接