Can't do #include <cmath>, fatal error: cmath: No such file or directory


I did all the steps, I added these lines to platformio.ini:

build_flags = -D USB_MIDI -D TEENSY_OPT_FASTEST -std=c++17
build_unflags = -std=gnu++11 -std=c++98

Then I got this error:

In file included from src\BoxLCD.cpp:1:0:
include/BoxLCD.h:7:17: fatal error: cmath: No such file or directory
compilation terminated.
*** [.pio\build\teensy2\src\BoxLCD.cpp.o] Error 1

I’m not trying to do anything fancier, I just want to use the pow() function!..

#include <cmath>

Nevertheless the PlatformIO seems to work exclusively like the old school C++!

Is there any way to use Modern C++ on PlatformIO?


PlatformIO gives you the same compiler and C++ standard the Arduino IDE wuold give you when compiling for that core, this is the only way to maintain compatibility.

What is the full platformio.ini?

; PlatformIO Project Configuration File


;   Build options: build flags, source filter

;   Upload options: custom upload port, speed and extra flags

;   Library options: dependencies, extra library storages

;   Advanced options: extra scripting


; Please visit documentation for the other options and examples


; [env:nanoatmega328]

; platform = atmelavr

; board = nanoatmega328

; framework = arduino

; monitor_speed = 115200

; debug_tool = avr-stub

; lib_deps =

;   fmalpartida/LiquidCrystal @ ^1.5.0

;   fortyseveneffects/MIDI Library @ ^5.0.2

;   jdolinay/avr-debugger@^1.4

;   ruiseixasm/Robust-EEPROM@^1.0


platform = teensy

board = teensy2

framework = arduino

; monitor_speed = 31250 ; Midi baud rate

monitor_speed = 57600

build_flags = -D USB_MIDI -D TEENSY_OPT_FASTEST -fexceptions -std=c++17

build_unflags = -fno-exceptions -std=gnu++11 -std=c++98

lib_extra_dirs = ~/Arduino/libraries

lib_ignore = MIDIUSB, Audio

lib_deps =

    fmalpartida/LiquidCrystal @ ^1.5.0


The Teensy 2 is a AVR based board a d uses a by default 5.4.1 avr-gcc, which I heavily doubt is capable of C++17. Also the AVR-GCC compilers are known for not supporting a whole lot of C++ standard library features, even in more recent versions.

So that, even with

platform = teensy
board = teensy2
framework = arduino
build_unflags = -fno-exceptions -std=gnu++11
build_flags = -D USB_MIDI -D TEENSY_OPT_FASTEST -fexceptions -std=gnu++17
; use a more recent 7.3.0 compiler instead of 5.4.1
platform_packages =

you won’t get exceptions (fails to find stdexcept) or cmatch. Even fill-in libraries like GitHub - mike-matera/ArduinoSTL: An STL and iostream implementation based on uClibc++ that supports my CS-11M class. currently fail to compile for the Teensy core because of a clashing declaration of the new operator.

So the only sensible thing I can recommend is to stick with the defaults, forget about using standards beyond C++11 or the C++ standard library on an AVR processor, and just use #include <math.h> instead of #include <cmath>. It has pow() and that compiles.