ChibiOS/RT with C++ STL

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: utzig, lbednarz, tfAteba, barthess, RoccoMarco

hazelnusse
Posts: 77
Joined: Thu May 24, 2012 8:01 am

ChibiOS/RT with C++ STL

Postby hazelnusse » Wed Nov 28, 2012 9:38 pm

Does anybody have example code that uses classes from the STL like std::list<>, std::vector<> from within a project that uses ChibiOS?

In my project I have simply started by adapting the ARMCM4-STM32F407-LWIP-FATFS-USB demo and adding new source files to the Makefile as needed. When I add code that uses the STL, it compiles, but I get these linker errors:

Code: Select all

In function `abort': abort.c:(.text.abort+0xa): undefined reference to `_exit'
In function `_kill_r': signalr.c:(.text._kill_r+0xe): undefined reference to `_kill'
In function `_getpid_r': signalr.c:(.text._getpid_r+0x0): undefined reference to `_getpid'

I'm using g++ as the linker rather than gcc. When I use gcc as the linker, I get different errors:

Code: Select all

In function `__gnu_cxx::new_allocator<std::_List_node<unsigned long> >::allocate(unsigned int, void const*)':
/home/hazelnusse/toolchain/lib/gcc/arm-none-eabi/4.7.3/../../../../arm-none-eabi/include/c++/4.7.3/ext/new_allocator.h:94: undefined reference to `operator new(unsigned int)'
build/obj/main.o: In function `_M_insert<long unsigned int>':
/home/hazelnusse/toolchain/lib/gcc/arm-none-eabi/4.7.3/../../../../arm-none-eabi/include/c++/4.7.3/bits/stl_list.h:1534: undefined reference to `std::__detail::_List_node_base::_M_hook(std::__detail::_List_node_base*)'

I'm not sure if I need to use g++ for the link step (I don't think I need C++ runtime support, but maybe things like std::list do?).

I tried to build the g++ demos (ARM7-LPC214x-G++, and ARMCM3-STM32F103-G++) using the ARM GNU embedded toolchain but both are failing on ch.cpp with the following error:

Code: Select all

Compiling ../../os/various/ch.cpp
../../os/various/ch.cpp: In static member function 'static eventmask_t chibios_rt::Event::ClearFlags(eventmask_t)':
../../os/various/ch.cpp:290:32: error: 'chEvtClearFlags' was not declared in this scope
../../os/various/ch.cpp: In static member function 'static eventmask_t chibios_rt::Event::AddFlags(eventmask_t)':
../../os/various/ch.cpp:295:30: error: 'chEvtAddFlags' was not declared in this scope
../../os/various/ch.cpp:296:3: warning: control reaches end of non-void function [-Wreturn-type]
../../os/various/ch.cpp: In static member function 'static eventmask_t chibios_rt::Event::ClearFlags(eventmask_t)':
../../os/various/ch.cpp:291:3: warning: control reaches end of non-void function [-Wreturn-type]
make: *** [build/obj/ch.o] Error 1

This is obviously not related to my issue but I thought I should point out that these two demos aren't building with a very standard toolchain.

If anybody is making extensive use of C++ in their embedded project with ChibiOS/RT, I'd love to see it.

Luke

User avatar
Giovanni
Site Admin
Posts: 13020
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 748 times
Been thanked: 621 times
Contact:

Re: ChibiOS/RT with C++ STL

Postby Giovanni » Wed Nov 28, 2012 9:43 pm

Hi,

I only tested without STL but I think you do need to link using g++-

Giovanni

mabl
Posts: 417
Joined: Tue Dec 21, 2010 10:19 am
Location: Karlsruhe, Germany
Been thanked: 1 time
Contact:

Re: ChibiOS/RT with C++ STL

Postby mabl » Wed Nov 28, 2012 9:49 pm

In addition to syscalls.{c,h} from ChibiOS, I use these:

Code: Select all

/*
 * stubs.c
 *
 *  Created on: Jul 10, 2011
 *      Author: mabl
 */

#include <stdio.h>

#include "ch.h"
#include "hal.h"

#include "stubs.h"

// FIXME: Stubs - HATE THEM
void _exit(int status){
   (void) status;
   chSysHalt();

   while(TRUE){}
}

pid_t _getpid(void){
   return 1;
}

void _kill(pid_t id){
   (void) id;
}

/* guard variables */
int __cxa_guard_acquire(__guard *g) {return !*(char *)(g);};
void __cxa_guard_release (__guard *g) {*(char *)g = 1;};
void __cxa_guard_abort (__guard * g __attribute__((unused))) {};



Code: Select all

/*
 * stubs.h
 *
 *  Created on: Aug 6, 2011
 *      Author: mabl
 */

#ifndef STUBS_H_
#define STUBS_H_

///* The ABI requires a 64-bit type.  */
__extension__ typedef int __guard __attribute__((mode (__DI__)));

int __cxa_guard_acquire(__guard *);
void __cxa_guard_release (__guard *);
void __cxa_guard_abort (__guard *);

void *__dso_handle = NULL;


#endif /* STUBS_H_ */



Far not perfect - but good enough for me. There are other quirks one have to remember when using C++, like exceptions being thrown by new() by default (can be overloaded). Also when creating static objects, the constructor is called while chibios is not running yet. Sometimes it's a good idea to move it the ChibiOS start to earlyinit.

hazelnusse
Posts: 77
Joined: Thu May 24, 2012 8:01 am

Re: ChibiOS/RT with C++ STL

Postby hazelnusse » Wed Nov 28, 2012 10:07 pm

mabl,
Do you link with g++ or gcc when you are using things from STL? When I add your stubs, I can get it to link with g++, but the code size goes from 28k to about 71kb for something as simple as:

Code: Select all

std::list<uint32_t> a;
a.push_back(1);

This amount of bloat isn't ok. I have exceptions and rtti turned off, so I don't know what else would cause all this bloat. Maybe the memory allocator provided by the standard library?

When I use your stubs and try to link with gcc, I get this error:

Code: Select all

build/obj/main.o: In function `__gnu_cxx::new_allocator<std::_List_node<unsigned long> >::allocate(unsigned int, void const*)':
/home/hazelnusse/toolchain/lib/gcc/arm-none-eabi/4.7.3/../../../../arm-none-eabi/include/c++/4.7.3/ext/new_allocator.h:94: undefined reference to `operator new(unsigned int)'
build/obj/main.o: In function `_M_insert<long unsigned int>':
/home/hazelnusse/toolchain/lib/gcc/arm-none-eabi/4.7.3/../../../../arm-none-eabi/include/c++/4.7.3/bits/stl_list.h:1534: undefined reference to `std::__detail::_List_node_base::_M_hook(std::__detail::_List_node_base*)'
collect2: error: ld returned 1 exit status
make: *** [build/robot.bike.elf] Error 1


Any thoughts how to fix that to see if I can use the STL without the bloat?

Thanks,
Luke

hazelnusse
Posts: 77
Joined: Thu May 24, 2012 8:01 am

Re: ChibiOS/RT with C++ STL

Postby hazelnusse » Wed Nov 28, 2012 10:40 pm

I have followed your directions, and additionally the directions here:
http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=59453

And now I can link with gcc with no errors, as long as I add -lstdc++ to the link step. The code bloat is now only 3kb larger (as opposed to 43kb before) if I add the two lines that declare and use a std::list<uint32_t>. This is still a non-trivial amount of bloat and I'll need to experiment with other things like <vector> to see how they compare.

How can I add a flag to the link step of the Makefile? I don't see it in the demo Makefiles so maybe it is buried somewhere else?

Thanks,
Luke

hazelnusse
Posts: 77
Joined: Thu May 24, 2012 8:01 am

Re: ChibiOS/RT with C++ STL

Postby hazelnusse » Wed Nov 28, 2012 10:44 pm

Actually, I apparently don't need to add the -lstdc++ to get it to build. I haven't tried running it yet, we'll see if it actually works.

gmb42
Posts: 31
Joined: Tue Oct 02, 2012 8:09 pm
Location: Aberdeenshire, UK

Re: ChibiOS/RT with C++ STL

Postby gmb42 » Wed Nov 28, 2012 11:09 pm

Not directly related to the issue in this thread, but it appears that the cpp wrapper hasn't been updated to take account of the event changes. I have trunk @ r4843 and get this when compiling the STM32F103-G++ demo:

Code: Select all

Compiling ../../os/various/ch.cpp
../../os/various/ch.cpp: In static member function 'static eventmask_t chibios_rt::Event::ClearFlags(eventmask_t)':
../../os/various/ch.cpp:290:32: error: 'chEvtClearFlags' was not declared in this scope
../../os/various/ch.cpp: In static member function 'static eventmask_t chibios_rt::Event::AddFlags(eventmask_t)':
../../os/various/ch.cpp:295:30: error: 'chEvtAddFlags' was not declared in this scope
../../os/various/ch.cpp:296:3: warning: control reaches end of non-void function [-Wreturn-type]
../../os/various/ch.cpp: In static member function 'static eventmask_t chibios_rt::Event::ClearFlags(eventmask_t)':
../../os/various/ch.cpp:291:3: warning: control reaches end of non-void function [-Wreturn-type]
make: *** [build/obj/ch.o] Error 1

User avatar
Giovanni
Site Admin
Posts: 13020
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 748 times
Been thanked: 621 times
Contact:

Re: ChibiOS/RT with C++ STL

Postby Giovanni » Thu Nov 29, 2012 8:43 am

Correct. the wrapper needs an update (or a rewrite...).

Giovanni

Freddie Chopin
Posts: 36
Joined: Wed Oct 17, 2012 8:57 am

Re: ChibiOS/RT with C++ STL

Postby Freddie Chopin » Fri Nov 30, 2012 10:40 pm

hazelnusse wrote:This amount of bloat isn't ok. I have exceptions and rtti turned off, so I don't know what else would cause all this bloat. Maybe the memory allocator provided by the standard library?

You may have exceptions disabled, but libstdc++ has them enabled internally anyway and that's the code you see being pulled. To completely disable exceptions you need a recompiled C++ library. You can find whole toolchain with libstdc++ without exceptions here:
https://github.com/freddiechopin/bleedi ... /downloads

I also have only the libs without exceptions for a previous version of linaro toolchain and a very old CS toolchain here:
http://www.freddiechopin.info/en/downlo ... ry/9-rozne

For now you "solved" your problem by overloading new and delete to not throw anything, but sooner or later you'll use something that throws a different exception and you won't be able to overload everything - and even if you could overloading library stuff is not the point of using STL.

4\/3!!

erfan
Posts: 14
Joined: Sat Apr 18, 2015 4:25 pm

Re: ChibiOS/RT with C++ STL

Postby erfan » Thu Jan 14, 2016 7:57 am

Hi,
Would somebody help me with including stl libraries? I want to include vector.h in main.cpp but Chibistudio couldn't find this library? It would be appreciated if tell me what changes I should consider?
Thanks


Return to “General Support”

Who is online

Users browsing this forum: No registered users and 8 guests