Error after using string

Hello, I’m trying to use string without Arduino.h file. I created this simply code. Unfortunately it is generating error.

#include <string.h>
void setup() 
{
String abca = "aba";
}

Error: error: ‘String’ was not declared in this scope

I can’t use Arduino.h because when I start unit tests on native platform I get an error.

In the Arduino Uno core files, the String class is defines in the header file WString.h.

That might help.

Cheers,
Norm.

@normandunbar It helps, but unfortunately I can’t compile this code under native platform. Is there any different option to resolve this problem. My must have is that this code has to compile under Arduino and native platform.

Hmm, you might be up that famous creek I suspect. The Arduino language has the String class, but its use is not advised by many online. Due to the amount of Static RAM it needs. String code can result in out of memory errors which will be very difficult to diagnose and fix.

If you absolutely must be able to compile the code in Arduino and Native, by which I assume you mean plain AVR C++, then write it all in AVR with a main() function, no setup() or loop(), and compile/test as native until it works.

Now, open the Arduino IDE and File->New. Edit->Select All, Edit->Cut. That clears the IDE.

Open your AVR file, select all, copy.

Paste into Arduino IDE. Save & compile.

The IDE will compile all the code, plus the Arduino framework, but the linker will not link to any of the Arduino core as you have defined a main() function. The code will obviously not be able to use Arduino functions in this mode.

Try it!

HTH

Cheers,
Norm.

Sorry, I didn’t specify what I mean. My must have is to compile this code for Arduino. I have to start unit tests on Arduino and native platform too.

In which case, I’m afraid, I don’t know what else to suggest. Sorry. :frowning_face:

Cheers,
Norm.

String is an Arduino.h class. There are raw char* as strings (for functions from string.h) and the std::string C++ datastructure (from #include <string>).

Confusion with strings and string size in C++ - #2 by maxgerhardt explains more.

1 Like