0

I have using C++ Eclipse in my STM32F103C8T6 with string type from std. I get error like: "region rom overflowed by 5980bytes".

#include "stm32f1xx.h"
#include <string>


std::string a;

int main(void)
{


    for(;;);
}

Is there any way to fix it on this microcontroler?

Bence Kaulics
  • 7,066
  • 7
  • 33
  • 63
mict
  • 11
  • 1
  • 3
  • The problem obviously is that your MC's ROM is to small for a program linking with the `libstd-c++`. – user0042 Aug 23 '17 at 18:41
  • I'm to suprise cause arduino is it almost whole on c++, but isnt working on much bigger microcontroller.. – mict Aug 23 '17 at 18:46
  • 1
    I didn't really investigate about that, but doesn't arduino come with a specialized C++ library? – user0042 Aug 23 '17 at 18:48
  • how much flash do you got? – Surt Aug 23 '17 at 18:53
  • Ok, i do not have using strictly this library. Did you know any method to comfortable and light work with strings? I depend use strings like string.lenght() and other... STM32F103C8T6 have 64kb flash. – mict Aug 23 '17 at 18:54
  • Well, maybe you could get the one that Arduino uses. – Bence Kaulics Aug 23 '17 at 19:59
  • Dynamic memory structures like `std::string` and `std::vector` are frowned upon in embedded system that have constrained memory limits. Search the internet for "c++ memory fragmentation". There is no garbage collection in C++. – Thomas Matthews Aug 23 '17 at 20:21
  • At my shop, we use an array of fixed capacity for the text. The input functions (USB and RS232) are designed to limit and not overflow the text array. – Thomas Matthews Aug 23 '17 at 20:23
  • @mict, the c library string functions are quite good enough to do light work on strings. The c library provided by ST even has a malloc() and free(), but I do not recommend using them. – Michaël Roy Aug 24 '17 at 17:49

2 Answers2

1

The STM32F103C8T6 only has 64 Kbytes of flash. Your program does not fit.

Look at the linker output, see how much you need. If possible, you can get the B version.
You could also see if you can enable the removal of unused objects. Since there might be functions of the string library that you don't even need.

stm32f103c8t6

*I'm taking a wild guess you're using GNU-ARM.

Jeroen3
  • 919
  • 5
  • 20
1

I think this question has been mostly answered here already but would like to throw in a couple words at a higher level perspective that might help some others going down this same path.

You can of course use C++ for microcontroller projects, but you'll want to avoid the majority of C++ paradigms. Want to be "C+"; closer to C than the Object Oriented Programming (OOP) side. In particular avoid most instanced things like in general C++ objects. And avoid code memory hogging paradigms like templates, etc.

You can setup a malloc(), so there is a C++ new/delete but most of the time you'll want to structure your code so there is next to no dynamic allocations. Not only because these add more code space bulk but you'll end up just using more of the limited 20kb RAM. Just to have a dynamic allocate you have to have some minimal linked list or similar to maintain the blocks of RAM et al.

Really, probably, you want to just use parts of the convince features of C++ like the loosening of variable scope restrictions, etc.

You'll probably want to just use the minimal C libraries that you can get on the ST website and/or other places.

For cheap low cost microcontroller projects, one of the arts of it is minification. Depending on the scope of you project of course. For something complex, with a lot of features, you might be hard pressed fitting everything in just the 64kb flash ROM space.

Some other options are getting a beefer microcontroller and/or adding an I2C EEPROM for more code space (that you'ed probably have to page in and out of RAM for running code use).

As a strategy, you'll probably just want to utilize the stack (local static sized character arrays) to use and manipulate dynamic strings and use good old C library types like strcpy(), sprintf(), etc.

Sirmabus
  • 636
  • 8
  • 8