Search code examples
c++c++11gccc++14arduino-ide

Getting Arduino IDE to compile for C++14


I've been looking to modify the build flags under Arduino's IDE 1.x, or even the Arduino CLI (which I haven't used but am willing to adopt) such that I can undefine -std=gnu++11 and instead define -std=gnu++14

I found a question related to this which gives me almost what I need:

Arduino 1.0.6: How to change compiler flag?

But it only shows how to add flags, not to remove them. I found another related post about changing arduino to GNU C++17 but the answer was it's not possible.

In this case, I know it's possible, as I do it in Platform IO in order to use the htcw_gfx library. It works great on most platforms that will run GFX reasonably anyway.

But I just don't know how to fiddle with Arduino to get it to dance the way I need to.

Any help would be greatly appreciated.


Solution

  • You can modify the default compile flags in the hardware/arduino/avr/platform.txt file.

    $ grep -n "std" hardware/arduino/avr/platform.txt
    23:compiler.c.flags=-c -g -Os {compiler.warning_flags} -std=gnu11 -ffunction-sections -fdata-sections -MMD -flto -fno-fat-lto-objects
    28:compiler.cpp.flags=-c -g -Os {compiler.warning_flags} -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto
    

    For some linux systems the following would work to automatically do this:

    dirname $(realpath $(which arduino)) | xargs -I{} sed -i "s/\(std=gnu++1\)1/\14/" {}/hardware/arduino/avr/platform.txt
    

    But this is not very portable, and will not work if the user has installed Arduino with Snap (as snap has these files mounted RO).

    Sources: