C++ and STM32

This forum is dedicated to feedback, discussions about ongoing or future developments, ideas and suggestions regarding the ChibiOS projects are welcome. This forum is NOT for support.
mabl
Posts: 417
Joined: Tue Dec 21, 2010 10:19 am
Been thanked: 1 time

C++ and STM32

Post by mabl »

Hello together,

I tried using C++ with a basic Chibios project. I therefore renamed main.c to main.cpp and moved it to CPPSRC in the makefile. I thought that should work but i get a

Code: Select all

arm-none-eabi/bin/ld: error: no memory region specified for loadable section `.ARM.extab'

error. Even though the linker script seems to define the .ARM.extab section. I'm a dumb beginner on linker scripts. Does anybody have a tip for me?
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: C++ and STM32

Post by Giovanni »

Probably it is just the linker script that must be updated, it worked with older GCC versions.

I will include a C++ demo in next release, I promised that to Roberto too.

Giovanni
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: C++ and STM32

Post by Giovanni »

Hi,

I made some tests, that section is for exception handling, just add -fno-exceptions to the makefile:

USE_CPPOPT = -fno-rtti -fno-exceptions

Supporting exceptions would require more changes but I would not recommend exceptions in an embedded application anyway because the code size would explode.
If you want to support exceptions change the file ch.ld as follow:

Code: Select all

/*
    ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
                 2011 Giovanni Di Sirio.

    This file is part of ChibiOS/RT.

    ChibiOS/RT is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    ChibiOS/RT is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
 * ST32F103 memory setup.
 */
__main_stack_size__     = 0x0400;
__process_stack_size__  = 0x0400;
__stacks_total_size__   = __main_stack_size__ + __process_stack_size__;

MEMORY
{
    flash : org = 0x08000000, len = 128k
    ram : org = 0x20000000, len = 20k
}

__ram_start__           = ORIGIN(ram);
__ram_size__            = LENGTH(ram);
__ram_end__             = __ram_start__ + __ram_size__;

SECTIONS
{
    . = 0;

    .text : ALIGN(16) SUBALIGN(16)
    {
        _text = .;
        KEEP(*(vectors))
        *(.text)
        *(.text.*)
        *(.rodata)
        *(.rodata.*)
        *(.glue_7t)
        *(.glue_7)
        *(.gcc*)
        *(.ARM.extab)
        *(.ARM.extab.*)
    } > flash

    .ctors :
    {
        PROVIDE(_ctors_start_ = .);
        KEEP(*(SORT(.ctors.*)))
        KEEP(*(.ctors))
        PROVIDE(_ctors_end_ = .);
    } > flash

    .dtors :
    {
        PROVIDE(_dtors_start_ = .);
        KEEP(*(SORT(.dtors.*)))
        KEEP(*(.dtors))
        PROVIDE(_dtors_end_ = .);
    } > flash

    .ARM.extab : {*(.ARM.extab* .gnu.linkonce.armextab.*)}

    __exidx_start = .;
    .ARM.exidx : {*(.ARM.exidx* .gnu.linkonce.armexidx.*)} > flash
    __exidx_end = .;

    .eh_frame_hdr : {*(.eh_frame_hdr)}

    .eh_frame : ONLY_IF_RO {*(.eh_frame)}

    . = ALIGN(4);
    _etext = .;
    _textdata = _etext;

    .data :
    {
        _data = .;
        *(.data)
        . = ALIGN(4);
        *(.data.*)
        . = ALIGN(4);
        *(.ramtext)
        . = ALIGN(4);
        _edata = .;
    } > ram AT > flash

    .bss :
    {
        _bss_start = .;
        *(.bss)
        . = ALIGN(4);
        *(.bss.*)
        . = ALIGN(4);
        *(COMMON)
        . = ALIGN(4);
        _bss_end = .;
    } > ram   
}

PROVIDE(end = .);
_end            = .;

__heap_base__   = _end;
__heap_end__    = __ram_end__ - __stacks_total_size__;


You will get some undefined symbol errors from the linker, basically you have to provide those functions to newlib (just stub them to chHalt()).

Giovanni
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: C++ and STM32

Post by Giovanni »

BTW, I just added a C++ demo for the STM32 to the repository, it should work with little changes on any Cortex-Mx MCU.

Giovanni
Dave_fr
Posts: 27
Joined: Thu Feb 17, 2011 11:26 pm

Re: C++ and STM32

Post by Dave_fr »

Hi,

I can't find any c++ demo in the repository ? Can you tell me what am I missing ?

BTW is there a "full" project example available somewhere ?

Dave
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: C++ and STM32

Post by Giovanni »

Hi Dave,

The demo is in the unstable version under ./demos/ARMCM3-STM32F103-G++, it is a full example and can be used as template for an application.

Giovanni
Cyrille
Posts: 26
Joined: Sun May 15, 2011 7:55 pm

Re: C++ and STM32

Post by Cyrille »

Hi,

I try to add a simple cpp file (Debug.cpp) in my STM32 project (composed by test.c file). I have the following error. The pb comes probably from the makeFile/compiler (see below). Does anyone have an idea ?

arm-none-eabi-gcc -c -mcpu=cortex-m3 -O2 -ggdb -fomit-frame-pointer -falign-functions=16 -ffunction-sections -fdata-sections -Wall -Wextra -Wstrict-prototypes -std=c99 -Wa,-alms=test.lst -DTHUMB_PRESENT -mno-thumb-interwork -DTHUMB_NO_INTERWORKING -MD -MP -MF .dep/test.o.d -mthumb -DTHUMB -I . -I../../os/ports/GCC/ARMCMx -I../../os/ports/GCC/ARMCMx/STM32 -I../../os/kernel/include -I../../os/hal/include -I../../os/hal/platforms/STM32 -I../../boards/MULTIPILOT32 -I../../os/various test.c -o test.o
In file included from test.c:5:0:
Debug.h:12:1: error: unknown type name 'class'
Debug.h:13:1: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
Debug.h:22:1: error: unknown type name 'Debug_Class'


Debug.cpp

Code: Select all


#ifndef DEBUG_H_
#define DEBUG_H_
#include <stdio.h>

class Debug_Class
{
public:
   Debug_Class();
   void putchar(uint8_t c);
   void putfloat(float x);
   void putlong(long x);
   void putint(int x);
};

extern Debug_Class Dbg;


#endif /* DEBUG_H_ */


MakeFile

Code: Select all

##############################################################################
# Build global options
# NOTE: Can be overridden externally.
#

# Compiler options here.
ifeq ($(USE_OPT),)
  USE_OPT = -O2 -ggdb -fomit-frame-pointer -falign-functions=16
endif

# C++ specific options here (added to USE_OPT).
ifeq ($(USE_CPPOPT),)
  USE_CPPOPT = -fno-rtti
endif

# Enable this if you want the linker to remove unused code and data
ifeq ($(USE_LINK_GC),)
  USE_LINK_GC = yes
endif

# If enabled, this option allows to compile the application in THUMB mode.
ifeq ($(USE_THUMB),)
  USE_THUMB = yes
endif

# Enable register caching optimization (read documentation).
ifeq ($(USE_CURRP_CACHING),)
  USE_CURRP_CACHING = no
endif

#
# Build global options
##############################################################################

##############################################################################
# Architecture or project specific options
#

# Enable this if you really want to use the STM FWLib.
ifeq ($(USE_FWLIB),)
  USE_FWLIB = no
endif

#
# Architecture or project specific options
##############################################################################

##############################################################################
# Project, sources and paths
#

# Define project name here
PROJECT = MP32TESTPROGRAM

# Define linker script file here
LDSCRIPT= ch.ld

# Imported source files
CHIBIOS = ../..
include $(CHIBIOS)/boards/MULTIPILOT32/board.mk
include $(CHIBIOS)/os/hal/platforms/STM32/platform.mk
include $(CHIBIOS)/os/hal/hal.mk
include $(CHIBIOS)/os/ports/GCC/ARMCMx/STM32/port.mk
include $(CHIBIOS)/os/kernel/kernel.mk
# include $(CHIBIOS)/test/test.mk

# C sources that can be compiled in ARM or THUMB mode depending on the global
# setting.
CSRC = $(PORTSRC) \
       $(KERNSRC) \
       $(TESTSRC) \
       $(HALSRC) \
       $(PLATFORMSRC) \
       $(BOARDSRC) \
       $(CHIBIOS)/os/various/evtimer.c \
       $(CHIBIOS)/os/various/syscalls.c \
       test.c


# C++ sources that can be compiled in ARM or THUMB mode depending on the global
# setting.
CPPSRC =

# C sources to be compiled in ARM mode regardless of the global setting.
# NOTE: Mixing ARM and THUMB mode enables the -mthumb-interwork compiler
#       option that results in lower performance and larger code size.
ACSRC =

# C++ sources to be compiled in ARM mode regardless of the global setting.
# NOTE: Mixing ARM and THUMB mode enables the -mthumb-interwork compiler
#       option that results in lower performance and larger code size.
ACPPSRC =

# C sources to be compiled in THUMB mode regardless of the global setting.
# NOTE: Mixing ARM and THUMB mode enables the -mthumb-interwork compiler
#       option that results in lower performance and larger code size.
TCSRC =

# C sources to be compiled in THUMB mode regardless of the global setting.
# NOTE: Mixing ARM and THUMB mode enables the -mthumb-interwork compiler
#       option that results in lower performance and larger code size.
TCPPSRC =

# List ASM source files here
ASMSRC = $(PORTASM)

INCDIR = $(PORTINC) $(KERNINC) $(TESTINC) \
         $(HALINC) $(PLATFORMINC) $(BOARDINC) \
         $(CHIBIOS)/os/various

#
# Project, sources and paths
##############################################################################

##############################################################################
# Compiler settings
#

MCU  = cortex-m3

#TRGT = arm-elf-
TRGT = arm-none-eabi-
CC   = $(TRGT)gcc
CPPC = $(TRGT)g++
# Enable loading with g++ only if you need C++ runtime support.
# NOTE: You can use C++ even without C++ support if you are careful. C++
#       runtime support makes code size explode.
LD   = $(TRGT)gcc
LD   = $(TRGT)g++
CP   = $(TRGT)objcopy
AS   = $(TRGT)gcc -x assembler-with-cpp
OD   = $(TRGT)objdump
HEX  = $(CP) -O ihex
BIN  = $(CP) -O binary

# ARM-specific options here
AOPT =

# THUMB-specific options here
TOPT = -mthumb -DTHUMB

# Define C warning options here
CWARN = -Wall -Wextra -Wstrict-prototypes -std=c99

# Define C++ warning options here
CPPWARN = -Wall -Wextra -std=c99

#
# Compiler settings
##############################################################################

##############################################################################
# Start of default section
#

# List all default C defines here, like -D_DEBUG=1
DDEFS =

# List all default ASM defines here, like -D_DEBUG=1
DADEFS =

# List all default directories to look for include files here
DINCDIR =

# List the default directory to look for the libraries here
DLIBDIR =

# List all default libraries here
DLIBS =

#
# End of default section
##############################################################################

##############################################################################
# Start of user section
#

# List all user C define here, like -D_DEBUG=1
UDEFS =

# Define ASM defines here
UADEFS =

# List all user directories here
UINCDIR =

# List the user directory to look for the libraries here
ULIBDIR =

# List all user libraries here
ULIBS =

#
# End of user defines
##############################################################################

ifeq ($(USE_FWLIB),yes)
  include $(CHIBIOS)/ext/stm32lib/stm32lib.mk
  CSRC += $(STM32SRC)
  INCDIR += $(STM32INC)
  USE_OPT += -DUSE_STDPERIPH_DRIVER
endif

include $(CHIBIOS)/os/ports/GCC/ARMCMx/rules.mk
 


Thanks
liamstask
Posts: 34
Joined: Wed Dec 08, 2010 5:42 pm

Re: C++ and STM32

Post by liamstask »

Hi Cyrille - looks like you are compiling that code as C, not C++. I would recommend putting that code into test.cpp instead of test.c and then, in the Makefile, adding it to the CPPSRC group instead of the CSRC group.
Cyrille
Posts: 26
Joined: Sun May 15, 2011 7:55 pm

Re: C++ and STM32

Post by Cyrille »

Hi Liamstak,

I have tested your recommendation and I have the following error :
c:/programmes/yagarto/bin/../lib/gcc/arm-none-eabi/4.6.0/../../../../arm-none-eabi/bin/ld.exe: error: no memory region specified for loadable section `.ARM.extab'
collect2: ld returned 1 exit status
liamstask
Posts: 34
Joined: Wed Dec 08, 2010 5:42 pm

Re: C++ and STM32

Post by liamstask »

Hi Cyrille - the same error was reported above. Please take a look at the previous responses, and hopefully you should be able to resolve the issue.
Post Reply