1. Introduction


CoIDE only supports the programming language C. It is possible to use C++ in CoIDE but this is officially not supported. This guide will show how to set up a C++ project. This is only for experienced users who prefer C++ over C. As C++ is not officially supported this is for reference only! An example is included with a C++ project with a blink-a-led using classes. It can be downloaded from our repository.
 
 
2. Setup a project for C++


To begin, it's needed to make a new project according to the tutorial explaining CoIDE (APN0008) Then right click the project and select "Configuration". In the overview windows add libstdc++ and libsupc++ to the linked libraries window:

 

In the build.xml file add the line " <fileset dir="." includes="**/*.cpp"/>" as pictured here:
 
 

Next, open the link.ld file and replace the following code:
 

   .text :
 
   {
 
       KEEP(*(.isr_vector .isr_vector.*))
 
       *(.text .text.* .gnu.linkonce.t.*)          
 
       *(.glue_7t) *(.glue_7)                        
 
       *(.rodata .rodata* .gnu.linkonce.r.*)                                
 
   } > rom
 

It has to be replaced with the following code:
 

.text :
 
{
 
KEEP(*(.isr_vector .isr_vector.*))
 
*(.text .text.* .gnu.linkonce.t.*)
 
*(.glue_7t) *(.glue_7)
 
*(.rodata .rodata* .gnu.linkonce.r.*)
 
 
 
/* C++ Static constructors/destructors (eabi) */
 
. = ALIGN(4);
 
KEEP(*(.init))
 
 
 
. = ALIGN(4);
 
__preinit_array_start = .;
 
KEEP (*(.preinit_array))
 
__preinit_array_end = .;
 
 
 
. = ALIGN(4);
 
__init_array_start = .;
 
KEEP (*(SORT(.init_array.*)))
 
KEEP (*(.init_array))
 
__init_array_end = .;
 
 
 
. = ALIGN(4);
 
KEEP(*(.fini))
 
 
 
. = ALIGN(4);
 
__fini_array_start = .;
 
KEEP (*(.fini_array))
 
KEEP (*(SORT(.fini_array.*)))
 
__fini_array_end = .;
 
 
 
/* C++ Static constructors/destructors (elf) */
 
. = ALIGN(4);
 
_ctor_start = .;
 
KEEP (*crtbegin.o(.ctors))
 
KEEP (*(EXCLUDE_FILE (*crtend.o) .ctors))
 
KEEP (*(SORT(.ctors.*)))
 
KEEP (*crtend.o(.ctors))
 
_ctor_end = .;
 
 
 
. = ALIGN(4);
 
KEEP (*crtbegin.o(.dtors))
 
KEEP (*(EXCLUDE_FILE (*crtend.o) .dtors))
 
KEEP (*(SORT(.dtors.*)))
 
KEEP (*crtend.o(.dtors))
 
} > rom
 
 
 
       Also the following code has to be added according to the picture:
 
   __exidx_start = .;
 
   .ARM.exidx :
 
   {
 
       *(.ARM.exidx* .gnu.linkonce.armexidx.*)
 
   } > rom
 
   __exidx_end = .;

Picture:
 
 
In the startup file the following code has to be added:

extern unsigned long __preinit_array_start;
 
extern unsigned long __preinit_array_end;
 
extern unsigned long __init_array_start;
 
extern unsigned long __init_array_end;
 
extern unsigned long _ctor_start;
 
extern unsigned long _ctor_end;
 
static void call_constructors(unsigned long *start, unsigned long *end) __attribute__((noinline));
 
static void call_constructors(unsigned long *start, unsigned long *end)
 
{
 
unsigned long *i;
 
void (*funcptr)();
 
for ( i = start; i < end; i++)
 
{
 
   funcptr=(void (*)())(*i);
 
   funcptr();
 
}
 
}
 
This has to be added under the line " extern void _eram; /*!< End address for ram*/". The startup file is called "startup_LPC17xx.c" and can be found in cmsis_boot --> startup. In the startup file add the following code between "SystemInit();" and "main();" :
 
//CoInitOS();   //only uncomment when using CoOS
 
//Call C++ global constructors
 
call_constructors(&__preinit_array_start, &__preinit_array_end);
 
call_constructors(&__init_array_start, &__init_array_end);
 
call_constructors(&_ctor_start, &_ctor_end);
 
After that the main.c has to be renamed to main.cpp because C++ is now used. Libraries from other tutorials can be used but every C file from the library has to be renamed to .cpp. So if the timer library is used timer.c has to be renamed to timer.cpp. Not all libraries are tested if they work in C++. An example project for a blink-a-led can be downloaded here The GPIO library is changed to support classes.


Copyright 2011. Joomla 1.7 templates - Joomla template maker. The Simplecortex is developed by BRC-Electronics