Conflict between std::abs() and macro for abs() in wiring.h (in library TeensyStep)

Hi everyone,

tl;dr
When running the code below, the macro for abs defined in wiring.h interferes with the use of std::abs().

I stumbled across an issue which leaves me baffled I must admit. I was working for the last 2 years on a project with stepper motors that make use of std functions and so far everything was fine. However, yesterday I decided to come up with a complete clean platformio environment and deleted the .platformio folder (stupid me). After re-installing the platformio-extension in VSCode I got plenty of compiler errors. It turned out that the #define abs in wiring.h is in conflict with std::abs() as it tries to replace abs with the macro, which does not make sense with regards to the std namespace. What I really don’t understand is, how is this now an issue and it has not been an issue before.

Unfortunately, I have no specifics about the platformio environment before the cleaning. The current (!) platformio dependencies and verbose build are shown below.

I understand that I could change all std::abs() to abs as a workaround but I really want to understand and solve the problem.

What am I missing here and why is there all of a sudden this issue?

I am very thankfull for any advice what could be going on.

Cheers,
Daniel

src/main.cpp

#include <Arduino.h>

void setup() {
    // abs(-6);        // macro from wiring.h
    // std::abs(-6);   // should work fine, but triggers compiler errors due to macro
    // std::cos(0.1);  // for testing that std functions work in general
}

void loop() {

}

platformio.ini

[env:teensy36]
platform = teensy
board = teensy36
framework = arduino

Dependencies

Resolving teensy36 dependencies...
Platform teensy @ 4.18.0 (required: teensy)
├── framework-arduinoteensy @ 1.158.0 (required: platformio/framework-arduinoteensy @ ~1.158.0)
└── toolchain-gccarmnoneeabi-teensy @ 1.110301.0 (required: platformio/toolchain-gccarmnoneeabi-teensy @ ~1.110301.0)

Verbose build

Processing teensy36 (platform: teensy; board: teensy36; framework: arduino)
-------------------------------------------------------------------------------------------------------------------------------------
CONFIGURATION: https://docs.platformio.org/page/boards/teensy/teensy36.html
PLATFORM: Teensy (4.18.0) > Teensy 3.6
HARDWARE: MK66FX1M0 180MHz, 256KB RAM, 1MB Flash
DEBUG: Current (jlink) External (jlink)
PACKAGES: 
 - framework-arduinoteensy @ 1.158.0 (1.58) 
 - toolchain-gccarmnoneeabi-teensy @ 1.110301.0 (11.3.1)
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
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\teensy36\src\main.cpp.o -c -fno-exceptions -felide-constructors -fno-rtti -std=gnu++14 -Wno-error=narrowing -fpermissive -Wall -ffunction-sections -fdata-sections -mthumb -mcpu=cortex-m4 -nostdlib -mfloat-abi=hard -mfpu=fpv4-sp-d16 -O2 -mno-unaligned-access -fsingle-precision-constant -DPLATFORMIO=60106 -D__MK66FX1M0__ -DARDUINO_TEENSY36 -DUSB_SERIAL -DARDUINO=10805 -DTEENSYDUINO=158 -DCORE_TEENSY -DF_CPU=180000000L -DLAYOUT_US_ENGLISH -Iinclude -Isrc -IC:\Users\Daniel\.platformio\packages\framework-arduinoteensy\cores\teensy3 src\main.cpp

Functions and macros defined in Arduino.h conflicting with standard functions are not uncommon, especially min, max, etc.

Judging from Releases · platformio/platform-teensy · GitHub, by deleting .platformio, you’ve enabled PlatformIO to download the current most recent version of the teensy platform (and with it Arduino core).

So just like the documentation says, use

platform = teensy@4.17.0

in the platformio.ini to downgrade to the previous version you were likely using.

If you want to get the definition issue of abs() solved at the root, you’ll have to take it up at Issues · PaulStoffregen/cores · GitHub.

Hi @maxgerhardt, thank you for your fast reply.

I already tried various platform versions. Starting from 4.18.0 down to 4.13.0. No version did resolve the issue regarding abs() and std::abs()

On another machine where I have the project code compiling (still successfully) I realised that it compiles differently even though platformio.ini is the exact same (different OS though).

Compiling the here mentioned code (on Win 11, e.g. with -nostdlib flag)

arm-none-eabi-g++ -o .pio\build\teensy36\src\main.cpp.o -c -fno-exceptions -felide-constructors -fno-rtti -std=gnu++14 -Wno-error=narrowing -fpermissive -Wall -ffunction-sections -fdata-sections -mthumb -mcpu=cortex-m4 -nostdlib -fsingle-precision-constant -mfloat-abi=hard -mfpu=fpv4-sp-d16 -O2 -mno-unaligned-access -fsingle-precision-constant -DPLATFORMIO=60106 -D__MK66FX1M0__ -DARDUINO_TEENSY36 -DUSB_SERIAL -DARDUINO=10805 -DTEENSYDUINO=157 -DCORE_TEENSY -DF_CPU=180000000L -DLAYOUT_US_ENGLISH -Iinclude -Isrc -IC:\Users\Daniel\.platformio\packages\framework-arduinoteensy@1.157.220801\cores\teensy3 src\main.cpp

Compiling a slightely different code making use of std::abs() with the same platformio.ini as above, but no conflict (e.g with flag -lstdc++ machine is a mac).

arm-none-eabi-g++ -o .pio/build/teensy36/firmware.elf -T mk66fx1m0.ld -Wl,--gc-sections,--relax -mthumb -mcpu=cortex-m4 -Wl,--defsym=__rtc_localtime=1681205658 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -O2 -fsingle-precision-constant .pio/build/teensy36/src/main.cpp.o -L/Users/michael/.platformio/packages/framework-arduinoteensy/cores/teensy3 -L.pio/build/teensy36 -Wl,--start-group .pio/build/teensy36/libFrameworkArduino.a -larm_cortexM4lf_math -lm -lstdc++ -Wl,--end-group

Okay, so what Teensy platform version is it using on the machine that can compile the code successfully?

The mac where it is building successfully is running platform = teensy@4.13.1

This is not related to PIO. The new toolchain and/or the new Teensyduino (1.58 / 1.59beta) reveiled a lot of those issues for other build systems as well. It looks like the recent changes somehow changed the order of includes which hid those issues in older toolchains. Which libraries show the effect?

Hi luni64,

well, it is actually your TeensyStep-Library we are using. I just created a plain new and empty Teensy 3.6 project in platformio (VSCode) and added TeensyStep as a lib dependency (see below).

As soon as I #include <TeensyStep> and build the compiler throws tons of errors, first of which is
ds = std::abs(targetPos - currentPos); in line 38 of LinStepAccelerator.h. Hovering over the function tells me, that the compiler is trying to insert the macro from wiring.h which is obviously not part of std. I assume all subsequent errors are due to wrong syntax of this one (except other potential usages of std).

[env:teensy36]
platform = teensy
board = teensy36
framework = arduino
lib_deps = luni64/TeensyStep@^2.1
Resolving teensy36 dependencies...
Platform teensy @ 4.18.0 (required: teensy)
├── framework-arduinoteensy @ 1.158.0 (required: platformio/framework-arduinoteensy @ ~1.158.0)
└── toolchain-gccarmnoneeabi-teensy @ 1.110301.0 (required: platformio/toolchain-gccarmnoneeabi-teensy @ ~1.110301.0)

Libraries
└── TeensyStep @ 2.1.0 (required: luni64/TeensyStep @ ^2.1)

I fixed that a couple of days ago in v2.3.3. Can you give that a try?

Edit: see PJRC (Teensy) Forum

Well that was very fast. Thank you. I used Release v2.3.3 · luni64/TeensyStep · GitHub as a normal project lib instead of adding the most recently published version via platformio lib_deps and it compiles fine.

I am going to mark your response as the solution to my problem even though I was expecting this to be a more general issue that would happen anywhere std ist used (for now). I also edited the title to make it a bit more related to the particular library.

Thanks again luni64!

Glad that it works now.
Of course this is a general issue. Since abs is #defined as a macro in wiring.h things like std::abs will not work after #including Arduino.h. I assume that some recent changes led to an inclusion of Arduino.h somewhere it wasn’t before. Anyway, pushing / undefining / popping the offending macros is probably the best solution to this issue.