Error: optional: No such file or directory (using GNU++17)

I’m trying to compile a C++17 project using std::optional for the Arduino Teensy. It works on my Linux machine, and on my Windows 10 VM, but I can’t get it compiling on my friend’s Windows 10 PC.

Can anyone suggest a solution to this?

The platformio.ini file;

build_unflags =
    -std=gnu++11 -std=c++11
    -std=gnu++14 -stc=c++14
build_flags =

platform = teensy
board = teensy40
framework = arduino

The main.cpp file;

#include <Arduino.h>
#include <optional>

auto GetOptional() -> std::optional<int>
  return 42;

void setup()
  auto value = GetOptional();

void loop()

The build error;

PS C:\Users\User\Documents\PlatformIO\Projects\cpp17test> C:\Users\User\.platformio\penv\Scripts\platformio.exe run -v -j1
Processing teensy40 (platform: teensy; board: teensy40; framework: arduino; build_unflags: -std=gnu++11 -std=c++11, -std=gnu++14 -std=c++14; build_flags: -std=gnu++17)
PLATFORM: Teensy (4.17.0) > Teensy 4.0
HARDWARE: IMXRT1062 600MHz, 512KB RAM, 1.94MB Flash 
DEBUG: Current (jlink) External (jlink)
 - framework-arduinoteensy @ 1.157.220801 (1.57)    
 - tool-teensy @ 1.157.0 (1.57) 
 - toolchain-gccarmnoneeabi @ 1.50401.190816 (5.4.1)
LDF: Library Dependency Finder ->
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 92 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
arm-none-eabi-g++ -o .pio\build\teensy40\src\main.cpp.o -c -std=gnu++17 -fno-exceptions -felide-constructors -fno-rtti -Wno-error=narrowing -fpermissive -fno-threadsafe-statics -Wall 
-ffunction-sections -fdata-sections -mthumb -mcpu=cortex-m7 -nostdlib -fsingle-precision-constant -mfloat-abi=hard -mfpu=fpv5-d16 -O2 -DPLATFORMIO=60113 -D__IMXRT1062__ -DARDUINO_TEENSY40 -DUSB_SERIAL -DARDUINO=10805 -DTEENSYDUINO=157 -DCORE_TEENSY -DF_CPU=600000000 -DLAYOUT_US_ENGLISH -Iinclude -Isrc -IC:\Users\User\.platformio\packages\framework-arduinoteensy\cores\teensy4 src\main.cpp
src\main.cpp:2:20: fatal error: optional: No such file or directory
compilation terminated.
*** [.pio\build\teensy40\src\main.cpp.o] Error 1
============================================================================= [FAILED] Took 2.13 seconds =============================================================================

The configuration and the source code compile for me without any problems.

Since I do not use the teensy platform, it was automatically installed on my system. It is therefore a fresh installation of the latest version, as no specific version of the Teensy platform is specified in platformio.ini.

Therefore, I suspect a faulty or outdated version on your friend’s PC.

Try the following:
On your friend’s PC:

  1. Close VS Code.
  2. Delete the folders
  • C:\User\<username>\.platformio\platforms\teensy
  • C:\User\<username>\.platformio\packages\tool-teensy
  • C:\User\<username>\.platformio\packages\framework-arduinoteensy
  • C:\User\<username>\.platformio\packages\toolchain-gccarmnoneeabi-teensy
  1. Start VS Code again.
    Wait until PlatformIO has reinstalled all required files.
    Then build the project again.

That’s got it. Thanks :slight_smile:

The failure here is that this is an old platform version. The newest is 4.18.0 and that one exactly updated the toolchain to v11.3.1, which likely made <optional> available. Your build log is clearly showing that toolchain-gccarmnoneeabi @ 1.50401.190816 (5.4.1) is used, very old compared to GC11.

Your bug is right there. You’re not pinning the platform version like the documentation says, hence, if someone has a previous version of the teensy platform installed, that one will just get used. However, since you require your code to use <optional>, this ends in a kaboom if the user doesn’t happen to have the very latest version installed. This line should be

platform = teensy@4.18.0

to make it compile all equally good on all computers, irregardless of previous installs.

1 Like