12

I have been writing a library for my project (for now I am using Arduino). The problem that I have is that string in C++ and in Arduino differ.

That is, I would like my library to be independent of Arduino, so I am using #include <string> and later declaring string s;. However in Arduino strings are defined by Arduino and declared String s2.

When I include my library to the sketch I get error: string: No such file or directory on the line where I try to include C++ string library (#include <string>).

Is there a way to make Arduino use C++ string library, or convert string to Arduino string when compiling?

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Tautvydas
  • 2,027
  • 3
  • 25
  • 38

2 Answers2

11

Several things:

  1. I am not sure about your rationale of doing something independent of Arduino. Usually, programming a micro-controller is not something very modulable and may be very different of a classical computer program. If you really want to be independent of any micro-controller and any platform, you can still use C-style strings, with a char * pointing to an array of chars.
  2. Doing what you would like to do is not easy at all. First of all you would need the source code of your std::string library. Doing an #include <string> is not enough: you must also compile the string library for the AVR platform.
  3. There are some "limitations" in the way Arduino process the C++ code as compared with a classical computer program: for instance, the operators new and delete are not implemented. Moreover, other things may be needed by the std::string implementation, so lots of dependencies to manage, or a code source size too big for Arduino.

There are some implementations of classical string tools for AVR microcontroller like Arduino, but they are done in a procedural way (and not in an object-oriented way) for C-style strings. For instance, for the avr-libc coming with avr-gcc, you can see the list of functions here: avr-libc string.h You even have the good old printf: avr-libc stdio.h

It is possible to use them with Arduino if you add the good header files in your code, but beware of the size of your code! Just using printf can increase the size by several kilobytes, which can be huge for a microcontroller depending on your needs.

To conclude, in my mind, the only portable way would be to use a char * string, http://arduino.cc/en/Reference/String

dda
  • 6,030
  • 2
  • 25
  • 34
Vincent Hiribarren
  • 5,254
  • 2
  • 41
  • 65
  • Thanks. I will try to use char* then, since I don't need a lot of strings. – Tautvydas Mar 24 '12 at 13:22
  • 1
    How can one implement C++ without `new` or `delete` operators? Wouldn't that break just about everything? – Michael Dorst Apr 29 '13 at 01:27
  • 5
    With C++, it is possible to create objects on the stack with automatic storage, while not using the heap. You just do not use pointers and "new" while creating objects.The object is then automaticall deleted at the end of the scope, like a classical primitive. With Arduino, it is why objects are generally created outside any functions in the global scope, and then intialized inside the setup() function (little is done in the constructor, an init() like function is prefered). They can then be used in the loop() function without having to pass any pointer references to loop(). – Vincent Hiribarren Apr 29 '13 at 05:49
2

The new SafeString Arduino library (available from the Library Manager) allows you to use 'string' type operations but without using new/delete. A detailed tutorial is available at https://www.forward.com.au/pfod/ArduinoProgramming/SafeString/index.html

Although the library uses Arduino interfaces like Print and Stream. It is straight forward to strip these out and make the library into a 'standard' C++ library, independent of Arduino.

drmpf
  • 31
  • 1