How to include the standard c++ library in platformio in vscode?

i am using arduino uno board.
I want to include “string” header file in my project, but i am not able to include the c++ std library. i also added “build_flags = -lstdc++” in the platformio.ini file.

2 Likes

Basically, most Microcontrollers do not support the STL. The compiler for your platform (for example avr) simply does not have the standard c++ library. You can add the STL through a library though, for example GitHub - maniacbug/StandardCplusplus: Standard C++ for Arduino (port of uClibc++) which also seems to be registered in PlatformIO: PlatformIO Registry.

Please consider that some things in the STL are not optimised for the use in Microcontrollers because of dynamic memory allocation. An example would be vector.

Have fun coding!

3 Likes

STL is not really made/optimized for a microcontroller’s tiny amount of RAM.

See here for more informations.

Then again, is depends on what project you’re working on. For example, if you’re designing something with lots of WS2812 (NeoPixels), you might want to save every bit, especially when working with Arduinos. If you use an ESP32 then you’re way much comfortable with its 512Kib of SRAM.

3 Likes

out of curiosity, why is platformio then using .cpp extentions? why not just use .c?

Well, you can still use c++ features such as classes etc. when you are writing something in a .cpp file. The c++ standard library just doesn’t make sense to include in microcontrollers most of the time. Like I said above, vectors and also maps can be really complex things for a small 8-bit microcontroller for example. So it kinda makes sense to not include the STL for those platforms. It is something compiler specific, and PlatformIO does not have anything to do with it. :smiley:

By using .cpp files rather than .c files you basically gain access to classes and templates, which is still kinda nice. Especially template programming is imo a great thing to have. :smiley:

1 Like

got it. i am also planning to use stm32h7 board which has 2mb ram. i wonder if i will be able to use std c++ library with it. but i think the stm cube ide use c programming instead of c++, don’t know about platformio support though.

got it. i am also planning to use stm32h7 board which has 1mb ram. i wonder if i will be able to use std c++ library with it. but i think the stm cube ide use c programming instead of c++, don’t know about platformio support though.

PlatformIO uses the C++ support of the underlying compiler.

You haven’t told us what initial board you were using. If it was an AVR board, the anaswers are right – STL support is limited but can be added with libraries.

For your H7 target: It should work without problems with arm-none-eabi-g++. Just make sure it uses the correct C++ version (Verbose build to check, build_unflags to remove old -std=c++XY flag, build_flags to add the new one).

4 Likes

ArduinoSTL may be worth a look if you intend to work with the Arduino framework … there is even mention of…

Strings and vectors both work, even with the limited amount of heap available to Arduino.

… which is most likely referring to the Arduino Uno, with it’s humongous 2k of SRAM… :wink:

1 Like

Oh and one last thing: while the regular STL might be one of the most thoroughly tested library ever, ArduinoSTL and other ports might not. Always use these ports with a lot of care.

2 Likes

Since most things are not tested like the stl library this isn’t really good advice. It probably works like most things in use in the open source world work.

Almost three years after the original post, I wonder if things have changed. I’m trying to use std::function and std::bind with board with:

[env]
framework = arduino
monitor_speed = 115200
upload_speed = 115200
lib_deps = 
	fastled/FastLED@^3.5.0
	jandrassy/ArduinoOTA@^1.0.9

[env:uno-wifi]
board = uno_wifi_rev2
platform = atmelmegaavr

If I try to:

#include <functional>

… yuck… squigglies.

I’m sorry to be needy, but I can’t find out how to tell platformio to use a newer compiler, and, of course, have no idea whether using a newer compiler would work at all.

Any advice would be very much appreciated.

Processing uno-wifi (board: uno_wifi_rev2; platform: atmelmegaavr; framework: arduino)
-----------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/atmelmegaavr/uno_wifi_rev2.html
PLATFORM: Atmel megaAVR (1.7.0) > Arduino Uno WiFi Rev2
HARDWARE: ATMEGA4809 16MHz, 6KB RAM, 47.50KB Flash
PACKAGES: 
 - framework-arduino-megaavr @ 1.8.7 
 - tool-avrdude-megaavr @ 1.60300.191015 (6.3.0) 
 - toolchain-atmelavr @ 1.70300.191015 (7.3.0)
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 9 compatible libraries
Scanning dependencies...
Dependency Graph
|-- FastLED @ 3.5.0
|   |-- SoftwareSerial @ 1.0
|   |-- SPI @ 1.0
|-- ArduinoOTA @ 1.0.9
|-- SPI @ 1.0
|-- Encoder
Building in release mode
Compiling .pio/build/uno-wifi/FrameworkArduinoVariant/variant.c.o
Compiling .pio/build/uno-wifi/lib4c3/FastLED/FastLED.cpp.o
Compiling .pio/build/uno-wifi/lib4c3/FastLED/bitswap.cpp.o
Compiling .pio/build/uno-wifi/lib4c3/FastLED/colorpalettes.cpp.o
Compiling .pio/build/uno-wifi/lib4c3/FastLED/colorutils.cpp.o
Compiling .pio/build/uno-wifi/lib4c3/FastLED/hsv2rgb.cpp.o
Compiling .pio/build/uno-wifi/lib4c3/FastLED/lib8tion.cpp.o
Compiling .pio/build/uno-wifi/lib4c3/FastLED/noise.cpp.o
Compiling .pio/build/uno-wifi/lib4c3/FastLED/platforms.cpp.o
Compiling .pio/build/uno-wifi/lib4c3/FastLED/platforms/esp/32/clockless_rmt_esp32.cpp.o
Compiling .pio/build/uno-wifi/lib4c3/FastLED/power_mgt.cpp.o
Compiling .pio/build/uno-wifi/lib4c3/FastLED/wiring.cpp.o
Compiling .pio/build/uno-wifi/src/main.cpp.o
Compiling .pio/build/uno-wifi/lib562/SoftwareSerial/SoftwareSerial.cpp.o
Compiling .pio/build/uno-wifi/lib848/SPI/SPI.cpp.o
Compiling .pio/build/uno-wifi/libd7a/ArduinoOTA/InternalStorage.cpp.o
Compiling .pio/build/uno-wifi/libd7a/ArduinoOTA/InternalStorageAVR.cpp.o
Compiling .pio/build/uno-wifi/libd7a/ArduinoOTA/InternalStorageESP.cpp.o
Compiling .pio/build/uno-wifi/libd7a/ArduinoOTA/InternalStorageRP2.cpp.o
Compiling .pio/build/uno-wifi/libd7a/ArduinoOTA/InternalStorageSTM32.cpp.o
Compiling .pio/build/uno-wifi/libd7a/ArduinoOTA/OTAStorage.cpp.o
In file included from src/main.cpp:18:0:
lib/Encoder/encoder.h: In member function 'void Encoder::attachInterruptHandler()':
lib/Encoder/encoder.h:109:30: error: 'bind' is not a member of 'std'
                         std::bind(&Encoder::handleInterrupt, this)
                              ^~~~
Compiling .pio/build/uno-wifi/libd7a/ArduinoOTA/WiFiOTA.cpp.o
Compiling .pio/build/uno-wifi/libd7a/ArduinoOTA/utility/rp2_flash_boot.c.o
Compiling .pio/build/uno-wifi/libd7a/ArduinoOTA/utility/stm32_flash_boot.c.o
*** [.pio/build/uno-wifi/src/main.cpp.o] Error 1
==================================== [FAILED] Took 0.83 seconds ====================================

It may be worth mentioning that I need bind and function – apparently – if I am to use attachInterrupt(…) in a class member function to attach another class member function as the interrupt handler. If you know of an example/project that’s doing this for deployment on Uno Wifi Rev2, please share. Embarrassingly… I’ve tried to figure out how to do this on and off over the last couple of months. I’ve had to give up on jousting this windmill a few times. Right now – tacky, tacky – I leave it to the user of the class to attach an interrupt handler that delegates to the. handleInterrupt() class function.

I was excited to see what seems to be great advice in How to assign a class method as an interrupt handler? - #2 by maxgerhardt but, alas, that apparently apply to the arduino esp support library. (Is it a tool-chain thing, or a framework thing, or a compiler thing, or… well, you get it. It’s mostly still gobbly-gook to me. I am learning, though not as fast as I would like to.

I’m about to check it in the garbage and use an ESP32-S3 dev kit. (I can get it to work there.) I had assumed the Arduino board would be ‘easier’ since it seems (or deemed) to be more mainstream. I’m beginning to think it’s more like a small estuary.